Print Pyramid Pattern in Python
Print a centered pyramid of stars using nested loops.
BeginnerLoop ProgramsExample 22 of 25
print-pyramid-pattern.py
Run in browser1# Program to print a pyramid star pattern23rows = int(input("Enter number of rows: "))45for 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.