Inverted Right-Angled Triangle Star Pattern in Python

What You'll Learn
This is the inverted version of Program 1. Start with rows stars on the first line and decrease by one star each row until you reach 1 star.
Total stars printed for rows = n is still \(n + (n-1) + \cdots + 1 = \frac{n(n+1)}{2}\).
⭐ 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(i):
print("*", end="")
print()🧠 How It Works
Setup
rows sets the first line width. for i in range(rows, 0, -1): counts i down from rows to 1 so the widest line prints first.
Stars with end=""
for _ in range(i): runs i times; each print("*", end="") adds another star on the same line.
New line
print() ends the row. The variation print("*" * i) is equivalent.
Inverted triangle
Total stars still n(n+1)/2 — O(n²) time, O(1) extra space. The widest line scrolls horizontally in the green preview on small screens.
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("*" * i)💡 Tips for Enhancement
Try These
- Use
"*" * ifor a shorter solution (shown in Example 2) - Validate input (reject
rows < 1) - Print the triangle with spaces between stars (e.g.,
"* " * i) - Compare it with Program 1 to see the mirror effect
- Try right-aligned versions next (Program 3)
Avoid
- Using the wrong range direction (you must count down)
- Forgetting
print()at the end of each row in the nested-loop version - Printing extra spaces at line ends (alignment issues in some consoles)
- Hardcoding input values when writing reusable code
- Assuming user input is always valid
Key Takeaways
Row i prints exactly i stars, starting from rows down to 1.
Total stars for n rows is \(n(n+1)/2\).
Python can print each row quickly using "*" * i.
Time complexity is O(n²) because total printed characters grow quadratically.
This is the mirror of Program 1.
❓ Frequently Asked Questions
rows to 1."* " * i instead of "*" * i. If you want to remove the last space, use ("* " * i).rstrip().n rows, because total printed characters are proportional to \(n(n+1)/2\).Next: Right-Aligned Triangle
Continue to Program 3 to print a right-aligned right-angled triangle.
In Python, counting down with range(rows, 0, -1) is the cleanest way to build inverted patterns.
9 people found this page helpful
