Pyramid Star Pattern in Python

Beginner
⏱️ 9 min read
📚 Updated: Aug 2025
🎯 2 Code Examples
2n − 1 stars in last row

What You'll Learn

This program prints a centered pyramid. Each next row adds 2 stars so the pyramid grows symmetrically.

For row i (starting at 1), print rows - i spaces and 2 * i - 1 stars.

⭐ Pattern Output

When you run the program with rows = 5:

Output
    *
   ***
  *****
 *******
*********
1

Complete Python Program

Fixed rows = 5 version:

Python
rows = 5

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

🧠 How It Works

1

Setup

rows is the pyramid height. for i in range(1, rows + 1): walks from one star on row 1 to a bottom row of 2 * rows - 1 stars.

Setup
2

Margin: print(" ", end="")

for _ in range(rows - i): prints rows - i spaces so the odd-width star block sits centered.

Centering
3

Stars: print("*", end="")

for _ in range(2 * i - 1): prints 1, 3, 5, …, 2*rows-1 asterisks—always odd so the pyramid has one peak star. String form: print(" " * (rows - i) + "*" * (2 * i - 1)).

Width
4

New line

print() ends the row. Row i prints (rows - i) + (2i - 1) = rows + i - 1 characters before the newline.

Line break
=

Symmetric pyramid

Total stars (sum of odd lengths 1 through 2n-1); O(n²) output for n = rows, O(1) extra space. The bottom row scrolls horizontally in the green preview on narrow screens. Same core idea as the top half of Program 10.

2

Variation — User Input Version

Read rows from user input:

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

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

💡 Tips for Enhancement

Try These

  • Print a hollow pyramid by printing stars only at the edges
  • Use a different character (like #)
  • Add spaces between stars (e.g., "* " * (2*i-1)) for a wider look
  • Validate input (reject rows < 1)
  • Try the inverted pyramid next (Program 6)

Avoid

  • Using even star counts (symmetry breaks)
  • Mixing tabs and spaces for alignment
  • Printing trailing spaces after the stars
  • Forgetting to reset row formatting each loop
  • Assuming user input is always valid

Key Takeaways

1

Row i prints rows - i spaces and 2 * i - 1 stars.

2

Odd star counts keep the pyramid symmetric.

3

The last row prints 2 * rows - 1 stars.

4

Time complexity is O(n²) due to printing \(\Theta(n^2)\) characters.

5

This pyramid is the foundation for diamond patterns (Programs 9 and 10).

❓ Frequently Asked Questions

It produces odd star counts (1, 3, 5, ...) so the pyramid has a single center and grows evenly on both sides.
Yes. Build the row as (" " * (rows - i)) + ("*" * (2*i - 1)) and print it.
It’s O(n²) for n rows, since total printed characters are \(\Theta(n^2)\).

Next: Inverted Pyramid Pattern

Continue to Program 6 to print the inverted pyramid in Python.

Program 6 →
Did you know?

If you remove the leading spaces, the pyramid becomes left-aligned and no longer looks centered.

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

9 people found this page helpful