Inverted Right-Aligned Right-Angled Triangle Star Pattern in Python

Beginner
⏱️ 9 min read
📚 Updated: Aug 2026
🎯 3 Code Examples
🚀 Live Preview
Spaces + Decreasing Stars

What You’ll Learn

The inverted right-aligned triangle keeps a flush right edge like Program 3, but star counts shrink like Program 2: widest line on top. This tutorial covers the space and star formulas, a live preview, algorithm steps, worked Python examples, edge cases, and complexity.

Shape Rule

Wide on top

First line has rows stars; each next line has one fewer, flush right.

Space Loop

i - 1

Print i - 1 spaces (none on row 1) so the block stays right-aligned.

Star Loop

i..rows

k from i to rows prints rows - i + 1 stars.

Fixed Width

n chars/row

Every row: (i - 1) + (rows - i + 1) = rows characters.

Live Preview

1–20 rows

Pick a row count and draw the inverted right-aligned triangle instantly.

O(n²)

Complexity

n rows × Θ(n) characters each — O(n²) time, O(1) extra space.

Introduction

An inverted right-aligned triangle has the widest star run on the first line and shrinks by one star each row, while the right edge stays flush.

It merges Program 2’s decreasing star counts with Program 3’s right alignment: growing spaces (i - 1) plus shrinking stars (rows - i + 1).

Why it matters?

It completes the four basic corner triangles (left / inverted / right / inverted-right). After this, centered pyramids are a small jump.

Key Highlights

Growing Spaces

Row i has i - 1 leading spaces.

Shrinking Stars

rows - i + 1 stars (or k = i..rows).

Same Right Edge

Flush-right like Program 3, inverted like Program 2.

Series Capstone

Last of the four basic right-angled variants.

In short: for each row i, print i - 1 spaces, then rows - i + 1 stars, then a newline.

📝 Problem & Approach

Given a positive integer rows, print an inverted right-aligned right-angled triangle of * characters with rows lines.

Python
# First 5 rows (spaces shown as ·)
# *****
# ·****
# ··***
# ···**
# ····*

Inputs & Outputs

ItemTypeDescription
rowsintNumber of lines (typically ≥ 1). Also the width of each line.
Printed outputtextRight-aligned rows: (i - 1) spaces + (rows - i + 1) stars.

Minimal workflow

Pseudocode
for i from 1 to rows:
    for j from 1 to (i - 1):     // range(1, i) in Python
        print " "
    for k from i to rows:        // rows - i + 1 stars
        print "*"
    print newline

Approach comparison

ApproachIdeaBest for
k = i..rowsStar loop bound encodes the countMatches classic textbook listings
1..(rows - i + 1)Explicit star count variableClearer when explaining formulas

⚡ Quick Reference

GoalPattern
Walk each rowfor i in range(1, rows + 1):
Leading spacesfor j in range(1, i): print(" ", end="")
Stars via rangefor k in range(i, rows + 1): print("*", end="")
Stars via countfor k in range(1, rows - i + 2):
Width check(i - 1) + (rows - i + 1) == rows
String shortcutprint(" " * (i - 1) + "*" * (rows - i + 1))

📋 Programs 1–4 at a Glance

Four corner variants — alignment and star growth differ.

Program 1
left grow

Only i stars — no spaces

Program 2
left shrink

Countdown / rows - i + 1 stars

Program 3
right grow

rows - i spaces, i stars

This page
right shrink

i - 1 spaces, rows - i + 1 stars

Context

When This Pattern Shows Up

Reach for this figure when combining right alignment with shrinking star counts.

  1. After Programs 2 and 3

    Natural merge of invert + right-align skills.

  2. Two-formula drills

    Spaces grow while stars shrink — opposite trends in one row.

  3. Pyramid stepping stone

    Next: Program 5 centers with odd star counts.

  4. Bound-style practice

    Compare k = i..rows with an explicit star count loop.

  5. Not a UI layout tool

    Console teaching pattern — not how you build app screens.

Key benefit: one pattern that locks in growing padding and shrinking fill while keeping a fixed right edge.

🔮 Live Preview

Choose a row count between 1 and 20 and draw the inverted right-aligned triangle in the browser.

Try 5, 7, or 10. The first line will have that many stars.

Live result
Press "Draw pattern".

Examples Gallery

Three complete Python programs — classic k = i..rows loops, console input, and a "*" * shortcut. Click View Output to reveal sample console results.

📚 Getting Started

Print five inverted right-aligned rows with nested loops.

Example 1 — Fixed rows = 5

Space loop with range(1, i), star loop with k from i to rows.

Python
rows = 5

for i in range(1, rows + 1):
    for j in range(1, i):
        print(" ", end="")
    for k in range(i, rows + 1):
        print("*", end="")
    print()

How It Works

When i = 1, print 0 spaces and stars for k = 1..5 (five stars). When i = 5, print 4 spaces and stars for k = 5..5 (one star). Every row has exactly 5 characters before the newline.

📈 Practical Variant

Let the user choose the height at runtime.

Example 2 — User Input Version

Read rows with input() and int() (wrap in try/except ValueError in real apps).

Python
rows = int(input("Enter the number of rows: "))

for i in range(1, rows + 1):
    for j in range(1, i):
        print(" ", end="")
    for k in range(i, rows + 1):
        print("*", end="")
    print()

How It Works

Same space/star core as Example 1; only the source of rows changes. Non-numeric input raises ValueError with bare int(input()) — use try/except for safer labs.

⚡ Shortcut Style

Same shape with explicit star count and string multiplication.

Example 3 — "*" * + Explicit Count

Build padding and stars from the formulas directly.

Python
rows = 5

for i in range(1, rows + 1):
    spaces = i - 1
    stars = rows - i + 1
    print(" " * spaces + "*" * stars)

How It Works

Naming spaces and stars makes the invert-vs-align story obvious. Keep the k = i..rows version when you want the classic listing style.

🧠 How the Algorithm Prints Rows

1

Set up

Set rows. Use i for the row, j for spaces, k for stars.

Setup
2

Outer loop

for i in range(1, rows + 1): — widest when i == 1, one star when i == rows.

Row
3

Indent spaces

for j in range(1, i): print(" ", end="") prints i - 1 spaces.

Align
4

Stars then newline

for k in range(i, rows + 1): print("*", end="") then print(). Width stays rows.

Stars
=

Inverted, still right-aligned

Star total n(n+1)/2; O(n²) time, O(1) extra space.

🔎 Worked Walkthrough — rows = 4

Trace spaces, stars, and total width for each outer-loop value of i.

iSpaces i - 1Stars rows - i + 1k rangePrinted row
1041..4****
2132..4 ***
3223..4  **
4314..4   *

Check: every row has width 4. Star total: 4+3+2+1 = 10.

Use Cases

Where this inverted right-aligned pattern shows up beyond the homework prompt.

1. Completing the Quartet

Finish left / invert / right / invert-right before pyramids.

Example: compare all four for rows = 5.

2. Opposite Trends

Spaces increase as stars decrease — strong formula practice.

Example: flip only one formula and watch the edge break.

3. Bound vs Count Style

Rewrite k = i..rows as 1..(rows - i + 1).

Example: same output, different loop headers.

4. Recover Program 3

Swap to rows - i spaces and 1..i stars.

Example: one edit each way between the two pages.

5. Pyramid Prep

Next patterns reuse padding plus multi-star runs.

Example: Program 5 uses 2*i - 1 stars.

6. Input Validation Labs

Pair with input validation and positive-row checks.

Example: reject rows <= 0 and re-prompt.

Pro Tip: say “Program 3 with star growth flipped” before coding — spaces grow, stars shrink.

Advantages

Why this pattern earns a place after Programs 1–3.

  1. 1. Combines Two Known Skills

    Invert (Program 2) + right-align (Program 3) in one figure.

  2. 2. Easy Width Check

    Every row length equals rows — bugs show up immediately.

  3. 3. Two Equivalent Star Loops

    Range style or count style — both are interview-friendly.

  4. 4. O(1) Extra Memory

    Streaming output needs only loop counters.

Pro Tip: lead with the formulas spaces = i - 1 and stars = rows - i + 1, then pick a loop style.

Usage Tips

Small habits that keep this pattern clean.

  1. 1. Keep range(1, i) for Spaces

    j <= i adds an extra space and shifts the right edge.

  2. 2. Use Real Spaces, Not Tabs

    Tabs break alignment across fonts and editors.

  3. 3. Wrap int(input()) in try/except

    Avoid crashes when the user types letters instead of a number.

  4. 4. Verify Width = rows

    Check tip and last rows: spaces + stars must sum to rows.

  5. 5. Dry-Run One Small n

    Trace rows = 4 on paper before coding larger demos.

Pro Tip: if the first line has leading spaces, your space formula is wrong — row 1 must use i - 1 = 0 spaces.

Common Pitfalls

Mistakes that commonly break inverted right-aligned triangles.

  1. 1. Using Program 3’s Formulas

    rows - i spaces and 1..i stars grows instead of shrinks.

    → Use i - 1 spaces and rows - i + 1 stars.

  2. 2. range(1, i + 1) Instead of range(1, i)

    One extra space per row shifts the right edge leftward.

    → Space loop must run exactly i - 1 times.

  3. 3. Star Loop 1..i

    That is Program 3 again — growing, not inverted.

    → Use k = i..rows or count rows - i + 1.

  4. 4. Mixing Tabs With Spaces

    Alignment looks fine in one editor and broken in another.

    → Always print the space character " ".

  5. 5. Blind int(input())

    Letters or empty input throw ValueError.

    → Catch ValueError and re-prompt on failure.

Edge Cases

Check these inputs before calling the solution done.

rows = 1

Single star

0 spaces + 1 star — same as the other triangle pages for n = 1.

rows = 0

Empty pattern

Outer loop never runs — print nothing or show a message.

Negative

rows < 0

Treat as invalid; re-prompt instead of silent empty output.

Large n

Wide first line

First line has n stars — fine for labs; may wrap on tiny terminals.

Bad input

Non-numeric ReadLine

int(input()) raises ValueError — validate first.

First row

i == 1

Space loop must not run; only a full run of stars.

🎯 Practice Problems

Try these variations to lock in the pattern.

1. Flip back to Program 3

  • Use rows - i spaces and 1..i stars
  • Confirm the staircase grows again

2. Rewrite the star loop

  • Replace k = i..rows with count rows - i + 1
  • Match Example 3’s clarity

3. Safe input loop

  • Validate input until rows >= 1
  • Then draw the pattern

4. Center pyramid next

  • Keep leading spaces; use odd star counts
  • Continue with Program 5

Notes

  • Program 3 flipped. Same right edge; star counts shrink instead of grow.
  • Every row has width rows: (i - 1) + (rows - i + 1) = rows.
  • Validate rows > 0 for interactive programs; rows = 1 prints a single star.
  • Next: Program 5 centers a full pyramid with 2 * i - 1 stars.

Quick Takeaway: print i - 1 spaces, then rows - i + 1 stars — inverted and flush right.

⏱️ Time and Space Complexity

ProgramTimeExtra space
Nested space/star loops (Examples 1–2)O(rows²)O(1)
"*" * shortcut (Example 3)O(rows²)O(rows) temporary per row string

Each of n rows prints Θ(n) characters (spaces + stars).

Wrap Up

🎉 Conclusion

The inverted right-aligned triangle is Program 3 with star growth flipped: i - 1 spaces and rows - i + 1 stars. With Programs 1–4 you can print any of the four basic right-angled variants.

Practice the three examples above, then continue to the center-aligned pyramid.

Spaces grow, stars shrink, width stays rows — keep range(1, i) for spaces, and validate row counts when reading input.

💡 Best Practices

✅ Do

  • Explain i - 1 spaces and rows - i + 1 stars before coding
  • Use range(1, i) (not range(1, i + 1)) for the space loop
  • Know both k = i..rows and the explicit count form
  • Check that each row has width rows
  • Use try/except ValueError for interactive demos

❌ Don’t

  • Reuse Program 3’s space/star formulas here
  • Add an extra space with j <= i
  • Print stars with 1..i when you meant inverted
  • Ignore bad console input in user-facing demos
  • Skip the rows = 1 edge case

Key Takeaways

Knowledge Unlocked

Five things to remember about this inverted right-aligned pattern

Print the flush-right inverted triangle the beginner-friendly way.

5
Core concepts
02

Spaces

i - 1

Formula
* 03

Stars

rows - i + 1

Formula
= 04

Width

Always rows

Check
O 05

Complexity

O(n²) time

Analysis

❓ Frequently Asked Questions

For each row i from 1 to rows, print i minus 1 spaces, then print stars with k running from i to rows inclusive. That prints rows minus i plus 1 stars. Row 1 has no spaces and rows stars; each later row adds one space and removes one star while keeping the same right edge.
The range i through rows has length rows minus i plus 1, which matches the star count. An equivalent loop is k from 1 to rows minus i plus 1, or k from rows down to i.
Program 3 uses (rows - i) spaces and stars 1 through i. Program 4 uses (i - 1) spaces and stars i through rows. Same right alignment; star counts grow in Program 3 and shrink in Program 4.
Program 2 is left-aligned with shrinking stars. Program 4 adds growing leading spaces so the same shrinking star counts stay flush on the right.
O(n²) for n rows. Each row prints on the order of n characters; there are n rows.
Yes. print(" " * (i - 1) + "*" * (rows - i + 1)) builds each row without explicit inner character loops.
range(1, i) prints exactly i - 1 spaces. Using range(1, i + 1) would add one extra space and break the right edge.
Wrap int(input()) in try/except ValueError so bad input does not crash the script.

Did you Know? 🔊

This pattern merges Program 2’s shrinking star count with Program 3’s right alignment. Every row still has width rows: (i - 1) + (rows - i + 1) = rows.

Continue to Center-Aligned Pyramid

Use leading spaces and odd star counts (2 * i - 1) to print a full pyramid.

Program 5 tutorial →

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.

12 people found this page helpful