Inverted Right-Aligned Star Pattern in Python

What You'll Learn
This program prints an inverted right-aligned triangle. The first row starts with zero spaces and prints rows stars, then each next row prints one more space and one fewer star.
For each row i from rows down to 1, print rows - i spaces, then print i 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(i):
print("*", end="")
print()🧠 How It Works
Setup
rows is the height. for i in range(rows, 0, -1): starts at the widest row and decreases i each line.
Padding: print(" ", end="")
for _ in range(rows - i): prints rows - i spaces. On the first row (i == rows) that count is zero; it grows as i falls.
Stars: print("*", end="")
for _ in range(i): prints i stars after the padding. Equivalent one-liner: print((" " * (rows - i)) + ("*" * i)).
New line
print() ends each row. Characters before the newline: (rows - i) + i = rows every time.
Inverted right-aligned triangle
Star total n(n+1)/2; O(n²) output for n = rows, O(1) extra space. Wide rows scroll horizontally in the green preview on phones.
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)) + ("*" * i))💡 Tips for Enhancement
Try These
- Use string repetition to simplify:
(" " * (rows - i)) + ("*" * i) - Print spaces between stars using
"* " * i(and trim if needed) - Compare with Program 3 to see the inverted mirror effect
- Try the pyramid next (Program 5)
- Validate input (reject
rows < 1)
Avoid
- Counting the outer loop in the wrong direction (it must count down)
- Using tabs for alignment (console rendering differs)
- Printing trailing spaces after stars
- Mixing indentation characters in the output logic
- Assuming user input is always valid
Key Takeaways
Row with star count i prints rows - i spaces first, then i stars.
Leading spaces increase by 1 each row to keep the triangle right-aligned.
Using range(rows, 0, -1) is the cleanest outer loop.
Time complexity is O(n²) due to printing \(\Theta(n^2)\) characters.
This is the inverted version of the right-aligned triangle (Program 3).
❓ Frequently Asked Questions
(" " * (rows - i)) + ("*" * i) for i from rows down to 1.n rows, since printing spaces + stars across all rows is \(\Theta(n^2)\).Next: Pyramid Pattern
Continue to Program 5 to print a pyramid star pattern in Python.
Many console alignment bugs come from mixing tabs and spaces—stick with spaces for predictable output.
9 people found this page helpful
