Inverted Pyramid Star Pattern in Python

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

What You'll Learn

This program prints an inverted centered pyramid. The first row has the maximum stars and each next row decreases the star count by 2.

For row i (counting down), 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(rows, 0, -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 height. for i in range(rows, 0, -1): starts with the widest line; inner loops match Program 5 (spaces then 2 * i - 1 stars), only the outer i sequence is reversed.

Setup
2

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

for _ in range(rows - i): prints rows - i spaces. As i decreases each row, that count grows so the shrinking star band stays centered.

Centering
3

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

for _ in range(2 * i - 1): prints an odd run that steps down (for rows = 5: 9, 7, 5, 3, 1). Same formula as Program 5; only i’s sequence changed.

Width
4

New line

print() ends each row. Per-row character count is still (rows - i) + (2i - 1) = rows + i - 1.

Line break
=

Inverted pyramid

Total stars still over n = rows rows; O(n²) time, O(1) extra space. The first line is the widest—it scrolls sideways in the green preview on small viewports.

2

Variation — User Input Version

Read rows from user input:

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

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

💡 Tips for Enhancement

Try These

  • Compare it with the pyramid (Program 5) to see the mirror effect
  • Use a different character (like #)
  • Add spaces between stars for a wider look
  • Validate input (reject rows < 1)
  • Combine Program 5 + Program 6 to make a diamond (Program 10)

Avoid

  • Using even star counts (symmetry breaks)
  • Mixing tabs and spaces for alignment
  • Forgetting to count down in the outer loop
  • Printing trailing spaces after the stars
  • Assuming user input is always valid

Key Takeaways

1

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

2

Leading spaces increase as stars decrease to keep the shape centered.

3

The first row prints 2 * rows - 1 stars.

4

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

5

This is the mirror of the pyramid (Program 5).

❓ Frequently Asked Questions

It preserves symmetry. You still want odd star counts; you’re just printing them in reverse order from largest to smallest.
Yes. Print (" " * (rows - i)) + ("*" * (2*i - 1)) for i from rows down to 1.
It’s O(n²) for n rows, since total printed characters are \(\Theta(n^2)\).

Next: Inverted V Hollow Pattern

Continue to Program 7 to print an inverted V-shaped hollow star pattern in Python.

Program 7 →
Did you know?

If you remove the leading spaces here, the inverted pyramid becomes left-aligned and loses its centered look.

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