Print Pyramid Pattern

Print pyramid pattern with stars.

Logic BuildingIntermediate
Logic Building
# Take n
n = int(input("Enter number of rows: "))

# Print pyramid
for i in range(1, n + 1):
    # Print spaces
    for j in range(n - i):
        print(" ", end="")
    # Print stars
    for j in range(2 * i - 1):
        print("*", end="")
    print()

Output

Enter number of rows: 5
    *
   ***
  *****
 *******
*********

Print spaces before stars for alignment.

Key Concepts:

  • Spaces: n - i
  • Stars: 2 * i - 1 (odd numbers)
  • Center alignment