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 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:
- We first ask the user to enter the size of the square.
- We use two nested
for
loops to iterate through the rows and columns of the square. - In the inner loop, we print a star followed by a space to separate the stars.
- 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