Python Star Pattern with While Loop

Print a star pattern using Python while loop with user input

Instead of a simple star pattern, let’s take user input to decide how many rows to print.

☆
☆☆
☆☆☆
☆☆☆☆
☆☆☆☆☆
i = int(input("Enter the number of rows: "))
star = 1
while True:
    print("☆" * star)
    if star == i:
        break
    star += 1
  • input() returns a string by default. Convert it to int.
  • while True creates an infinite loop.
  • Each iteration prints stars and increments by 1. When it matches the input, break stops the loop.

Result

Entering 7 prints stars from 1 to 7 rows.

Built with Hugo
Theme Stack designed by Jimmy