Inverted Right-Angled Triangle Star Pattern in Python

Beginner
⏱️ 8 min read
📚 Updated: Aug 2025
🎯 2 Code Examples
n(n+1)/2 stars total

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:

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

Complete Python Program

Fixed rows = 5 version:

Python
rows = 5

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

🧠 How It Works

1

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.

Setup
2

Stars with end=""

for _ in range(i): runs i times; each print("*", end="") adds another star on the same line.

Inner loop
3

New line

print() ends the row. The variation print("*" * i) is equivalent.

Line break
=

Inverted triangle

Total stars still n(n+1)/2O(n²) time, O(1) extra space. The widest line scrolls horizontally in the green preview on small screens.

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("*" * i)

💡 Tips for Enhancement

Try These

  • Use "*" * i for 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

1

Row i prints exactly i stars, starting from rows down to 1.

2

Total stars for n rows is \(n(n+1)/2\).

3

Python can print each row quickly using "*" * i.

4

Time complexity is O(n²) because total printed characters grow quadratically.

5

This is the mirror of Program 1.

❓ Frequently Asked Questions

Yes. The only change is the direction of the outer loop: count down from rows to 1.
Print "* " * i instead of "*" * i. If you want to remove the last space, use ("* " * i).rstrip().
It’s O(n²) for 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.

Program 3 →
Did you know?

In Python, counting down with range(rows, 0, -1) is the cleanest way to build inverted patterns.

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