Inverted Pyramid Star Pattern in Python

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:
*********
*******
*****
***
*Complete Python Program
Fixed rows = 5 version:
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
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.
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.
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.
New line
print() ends each row. Per-row character count is still (rows - i) + (2i - 1) = rows + i - 1.
Inverted pyramid
Total stars still n² 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.
Variation — User Input Version
Read rows from user input:
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
Row with index i prints rows - i spaces and 2 * i - 1 stars.
Leading spaces increase as stars decrease to keep the shape centered.
The first row prints 2 * rows - 1 stars.
Time complexity is O(n²) due to printing \(\Theta(n^2)\) characters.
This is the mirror of the pyramid (Program 5).
❓ Frequently Asked Questions
(" " * (rows - i)) + ("*" * (2*i - 1)) for i from rows down to 1.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.
If you remove the leading spaces here, the inverted pyramid becomes left-aligned and loses its centered look.
9 people found this page helpful
