Print Pyramid Pattern in Python

Print a centered pyramid of stars using nested loops.

BeginnerLoop ProgramsExample 22 of 25
print-pyramid-pattern.py
1# Program to print a pyramid star pattern
2
3rows = int(input("Enter number of rows: "))
4
5for i in range(1, rows + 1):
6 spaces = " " * (rows - i)
7 stars = "*" * (2 * i - 1)
8 print(spaces + stars)

Output

Enter number of rows: 3
  *
 ***
*****

What's going on

We center each row by printing leading spaces and then an odd number of stars: 2*i - 1.

After this program

If the idea is still fuzzy, read Day 9: Loops and Iteration in the Python course. For a short check, try the Python quiz (300 questions). Stay with all 25 loop programs, or see every topic on Python exercises.