Python Program Print Square Of Stars

By | October 30, 2023

Python Program Print Square Of Stars – To print a square of stars in Python, you can use nested loops. Here’s a simple example that allows you to specify the size of the square:

Python Program Print Square Of Stars

Python Program Print Square Of Stars

Python Source Code

# Get the size of the square from the user
size = int(input("Enter the size of the square: "))

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

Output

Enter the size of the square: 7
* * * * * * * 
* * * * * * * 
* * * * * * * 
* * * * * * * 
* * * * * * * 
* * * * * * * 
* * * * * * * 

In this program:

  1. We first ask the user to enter the size of the square.
  2. We use two nested for loops to iterate through the rows and columns of the square.
  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 size variable to control the size of the square. For example, if the user enters 5, the program will print a 5×5 square of stars.

Python Print Star Patterns 14 Programs

Loading

Leave a Reply

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