Using nested Loops to print a Rectangle in Python

avatar
Borislav Hadzhiev

Last updated: Apr 11, 2024
4 min

banner

# Using nested loops to print a rectangle in Python

To use nested loops to print a rectangle:

  1. Use a for loop to iterate over a range object of length N rows.
  2. Use a nested for loop to iterate over a range object of length N columns.
  3. Print an asterisk for each column.
main.py
num_rows = 2 num_cols = 3 for i in range(num_rows): for i in range(num_cols): print('*', end=' ') print('')

print rectangle with nested loops

The code for this article is available on GitHub

The num_rows variable stores the number of rows the rectangle should have and the num_cols variable stores the number of columns.

We used a for loop to iterate over a range object of length N rows.

The range() class is commonly used for looping a specific number of times in for loops

If you only pass a single argument to the range() constructor, it is considered to be the value for the stop parameter.

main.py
print(list(range(3))) # [0, 1, 2] print(list(range(5))) # [0, 1, 2, 3, 4]

The range starts at 0 and goes up to, but not including the specified number.

On each iteration of the outer loop, we use a nested loop to iterate over a range object of length N columns.

Notice that we used the same name for the variable in the outer and inner loops.

main.py
num_rows = 2 num_cols = 3 for i in range(num_rows): for i in range(num_cols): print('*', end=' ') print('')

We used the print() function to print an asterisk for each column and set the end argument to a space.

The end argument is printed at the end of the message.

By default, end is set to a newline character (\n).

The end argument has to be set to a space, so we can print multiple asterisks on the same row (one for each column).

The last step is to print an empty string so that the next asterisk is printed on the next line.

You can also extract the logic into a reusable function.

main.py
def print_rectangle(num_rows, num_cols): for _i in range(num_rows): for _i in range(num_cols): print('*', end=' ') print('') print_rectangle(2, 3) print('x' * 50) print_rectangle(3, 4) print('x' * 50) print_rectangle(4, 5)

print rectangle with nested loops using reusable function

The code for this article is available on GitHub

The print_rectangle function takes the number of rows and the number of columns as parameters and prints a rectangle with the specified rows and columns.

# Using a more complex approach to print a rectangle with nested loops

You can also use an alternative, more complex approach to print a rectangle with nested loops.

  1. Use a for loop to iterate over a range object of length N rows.
  2. Print an asterisk for each row.
  3. Use a nested for loop to iterate over a range object of length N columns - 1.
  4. Print an asterisk for each column.
main.py
num_rows = 2 num_cols = 3 for i in range(num_rows): print('*', end=' ') for j in range(num_cols-1): i *= j print('*', end=' ') print('')

using nested loops to print rectangle in python

The code for this article is available on GitHub

On each iteration of the outer for loop, we print an asterisk.

main.py
num_rows = 2 num_cols = 3 for i in range(num_rows): print('*', end=' ') for j in range(num_cols-1): i *= j print('*', end=' ') print('')

The end argument is printed at the end of the message.

We set the argument to a space to be able to print multiple asterisks on the same row.

By default, end is set to a newline character (\n).

The next step is to use a nested for loop to iterate for the columns.

Notice that we subtract 1 from the number of columns when calling the range() class.

This is necessary because the asterisk * for the first column is already printed by the outer for loop.

On each iteration, we set the value of i to be equal to the result of multiplying i by j.

The nested for loop also prints an asterisk for each column.

The last step is to print an empty string so that the next row starts on a new line.

The default behavior of the print() function is that consecutive calls are displayed on a new line but we set the end argument to a space to be able to display multiple columns on the same row.

# Creating a reusable function that uses nested loops to print a rectangle

You can also create a reusable function that uses nested loops to print a rectangle.

main.py
def print_rectangle(num_rows, num_cols): for i in range(num_rows): print('*', end=' ') for j in range(num_cols-1): i *= j print('*', end=' ') print('') print_rectangle(2, 3) print('x' * 50) print_rectangle(3, 4) print('x' * 50) print_rectangle(4, 5)

define reusable function that uses nested loops to print rectangle

The code for this article is available on GitHub

The print_rectangle function takes the number of rows and the number of columns as parameters and prints a rectangle with the specified rows and columns.

# Additional Resources

You can learn more about the related topics by checking out the following tutorials:

I wrote a book in which I share everything I know about how to become a better, more efficient programmer.
book cover
You can use the search field on my Home Page to filter through all of my articles.