Print Pyramid Pattern

Print a centered pyramid of stars using nested loops.

PythonBeginner
Python
# Program to print a pyramid star pattern

rows = int(input("Enter number of rows: "))

for i in range(1, rows + 1):
    spaces = " " * (rows - i)
    stars = "*" * (2 * i - 1)
    print(spaces + stars)

Output

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

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