Python Program Print Stars Rectangle

By | October 30, 2023

Python Program Print Stars Rectangle – To print a rectangle of stars in Python, you can use nested loops similar to the square example, but you’ll need to specify the number of rows and columns for the rectangle.

Source code

# Get the number of rows and columns from the user
rows = int(input("Enter the number of rows: "))
columns = int(input("Enter the number of columns: "))

# Nested loop to print the rectangle of stars
for i in range(rows):
    for j in range(columns):
        print("*", end=" ")
    print()

Here’s a simple program to print a rectangle of stars:

Python Program Print Rectangle of Stars

Python Program Print Rectangle of Stars

Output
Enter the number of rows: 5
Enter the number of columns: 10
* * * * * * * * * *
* * * * * * * * * *
* * * * * * * * * *
* * * * * * * * * *
* * * * * * * * * *In this program:

  1. We ask the user to enter the number of rows and columns for the rectangle.
  2. We use two nested for loops to iterate through the rows and columns of the rectangle.
  3. In the inner loop, we print a star followed by a space to separate the stars.
  4. After each row is printed (the inner loop completes), we use print() to move to the next line, so the stars are printed in rows.

You can adjust the rows and columns variables to control the dimensions of the rectangle. For example, if the user enters 3 for rows and 5 for columns, the program will print a rectangle of stars with 3 rows and 5 columns.

Loading

Leave a Reply

Your email address will not be published. Required fields are marked *