In this shot, we will discuss how to generate a hollow rectangle pattern using stars in Python.
Numerous patterns can be printed in Python once you have a strong grip over loops. Here, we will use simple for
loops to generate a rectangle pattern with stars.
To execute a rectangular pattern with Python programming, we will use 2 for
loops - one outer and one nested loop:
Let us look at the code snippet below.
# Initialising Length and Breadthrows = 3columns = 6# Loop through number of rowsfor i in range(rows):# Loop through number of columnsfor j in range(columns):# Printing Patternif(i == 0 or i == rows - 1 or j == 0 or j == columns - 1):print('*', end = ' ')else:print(' ', end = ' ')print()
for
loop to iterate through the number of rows.for
loop to iterate through the number of columns.i == 0
creates the upper side of the rectangle.i == rows - 1
creates the upper side of the rectangle.j == 0
creates the left side of the rectangle.j == columns - 1
creates the right side of the rectangle.end
statement is used to stay on the same line.print()
statement is used to move to the next line.