Inverted Right-Aligned Star Pattern in Python

Beginner
⏱️ 8 min read
📚 Updated: Aug 2025
🎯 2 Code Examples
spaces + stars per row

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:

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(i):
        print("*", end="")
    print()

🧠 How It Works

1

Setup

rows is the height. for i in range(rows, 0, -1): starts at the widest row and decreases i each line.

Setup
2

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.

Right align
3

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

for _ in range(i): prints i stars after the padding. Equivalent one-liner: print((" " * (rows - i)) + ("*" * i)).

Stars
4

New line

print() ends each row. Characters before the newline: (rows - i) + i = rows every time.

Line break
=

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.

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

1

Row with star count i prints rows - i spaces first, then i stars.

2

Leading spaces increase by 1 each row to keep the triangle right-aligned.

3

Using range(rows, 0, -1) is the cleanest outer loop.

4

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

5

This is the inverted version of the right-aligned triangle (Program 3).

❓ Frequently Asked Questions

Because the number of leading spaces increases by 1 each row, so the last star column stays fixed on the right.
Yes. Print (" " * (rows - i)) + ("*" * i) for i from rows down to 1.
It’s O(n²) for 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.

Program 5 →
Did you know?

Many console alignment bugs come from mixing tabs and spaces—stick with spaces for predictable output.

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