Trusted answers to developer questions
Trusted Answers to Developer Questions

Related Tags

communitycreator
python

How to generate a rectangle pattern using stars in Python

Vinisha Maheshwari

In this shot, we will discuss how to generate a rectangle pattern using stars in Python. Numerous patterns can be printed using python, once we have a strong grip over the concepts involving loops. Here we will be using simple for loops to generate a rectangle pattern using stars.

Description

To execute a rectangular pattern using Python programming, we will use 2 for loops:

  • Outer loop: It is used to iterate over the number of rows.
  • Inner nested loop: It is used to iterate over the number of columns in each row.

Code

Let’s look at the code snippet below.

# Initialising Length and Breadth
rows = 3
columns = 6

# Loop through number of rows
for i in range(rows):
    
    # Loop through number of columns
    for j in range(columns):
        
        # Printing Pattern
        print('*', end = '  ')
    print()

Explanation

  • In line 2, we take the input for the number of rows.
  • In line 3, we take the input for the number of columns.
  • In line 6, we create a for loop to iterate through the number of rows.
  • In line 9, we create a for loop to iterate through the number of columns.
  • In lines 12 and 13, we print the pattern.
    • The end statement is used to stay on the same line.
    • The print() statement is used to move to the next line.

RELATED TAGS

communitycreator
python
RELATED COURSES

View all Courses

Keep Exploring