Python Inverted Triangle Star Pattern (Right-Aligned)
Beginner
6 min read
Updated: Sep 2026
3 programs
Live preview
Definition
What Is This Pattern?
An inverted right-aligned triangle shrinks star counts while staying flush on the right: row i has i - 1 leading spaces and rows - i + 1 stars.
Remember
Rule: spaces = i - 1, stars = rows - i + 1
*****
****
***
**
* ← 5 rows (spaces shown as blanks)
It combines Program 2’s shrinking stars with Program 3’s right alignment. Every row still has width rows before the newline.
Approach
How to Solve It
Two inner loops per row — spaces then stars — or the same formulas with string multiplication.
Method
Idea
Best for
Nested loops
range(i - 1) spaces, then k = i..rows stars
Learning, interviews, exams
String multiply
Build spaces and stars as whole strings
Shorter demos once loops click
Pseudocode
Pseudocode
for i from 1 to rows:
for j from 1 to i - 1: // i - 1 spaces
print " " (no newline)
for k from i to rows: // rows - i + 1 stars
print "*" (no newline)
print newline
Cheat sheet
Goal
Pattern
Walk each row
for i in range(1, rows + 1):
Leading spaces
for j in range(i - 1): print(" ", end="")
Shrinking stars
for k in range(i, rows + 1): print("*", end="")
Star count form
for k in range(1, rows - i + 2):
Fixed width check
(i - 1) + (rows - i + 1) == rows
Row shortcut
print(" " * (i - 1) + "*" * (rows - i + 1))
Printing Stars vs Starting a New Line
API
Effect
Use for
print(..., end="")
Stays on the same line
Each space and each *
print()
Ends the current line
After spaces and stars for that row
Print characters without a newline, then end the row once.
Try it
Live Preview
Change the row count and the inverted right-aligned triangle updates instantly.
Whole numbers from 1 to 20. Each row has width rows (spaces + stars).
Live result5 rows · 15 stars
*****
****
***
**
*
Trace
Worked Walkthrough — rows = 4
Trace spaces, stars, and total width for each outer-loop value of i.
i
Spaces i - 1
Stars rows - i + 1
Width
Printed row
1
0
4
4
****
2
1
3
4
***
3
2
2
4
**
4
3
1
4
*
Total stars: 4 + 3 + 2 + 1 = 10 = 4×5/2. Width stays 4 on every row.
Code
Python Programs
Three complete programs: fixed rows, input(), and a string-multiply shortcut. Use View Output to reveal sample results.
Example 1 — Fixed rows = 5
Hard-coded height — ideal for first demos and screenshots.
Python
rows = 5
for i in range(1, rows + 1):
for j in range(i - 1):
print(" ", end="")
for k in range(i, rows + 1):
print("*", end="")
print()
Output
*****
****
***
**
*
How It Works
1. Set height.rows = 5 means five lines, each of width 5.
2. Outer loop picks the row.i runs from 1 to rows.
3. Spaces then stars. Print i - 1 spaces (range(i - 1)), then stars for k = i..rows (that is rows - i + 1 stars).
4. Break the line. Bare print() after both inner loops starts the next row.
When i = 1: 0 spaces + 5 stars. When i = 5: 4 spaces + 1 star.
Example 2 — User Input Version
Read the row count at runtime. Prefer a try / except in real apps (shown in the tip below).
Python
rows = int(input("Enter the number of rows: "))
for i in range(1, rows + 1):
for j in range(i - 1):
print(" ", end="")
for k in range(i, rows + 1):
print("*", end="")
print()
Output (when user enters 4)
Enter the number of rows: 4
****
***
**
*
How It Works
1. Prompt and read.input() returns a string; int(...) converts it to a whole number.
2. Same nested-loop core. Only the source of rows changes — the print logic matches Example 1.
3. Safer input tip.int(input()) raises ValueError on letters. Prefer:
Safer input
raw = input("Enter the number of rows: ").strip()
try:
rows = int(raw)
except ValueError:
print("Enter a positive whole number.")
raise SystemExit
if rows < 1:
print("Enter a positive whole number.")
raise SystemExit
Example 3 — String Multiply + Explicit Count
Name the space and star counts, then build each row in one call.
Python
rows = 5
for i in range(1, rows + 1):
spaces = i - 1
stars = rows - i + 1
print(" " * spaces + "*" * stars)
Output
*****
****
***
**
*
How It Works
1. Compute both counts.spaces = i - 1 and stars = rows - i + 1 make the invert-and-align rule obvious.
2. Build and print. Concatenate the space and star strings, then print(...) ends the line.
3. Learn loops first. Use Examples 1–2 when you need to show nested bounds; treat this as a polish shortcut afterward.
Edge Cases & Pitfalls
Check these before calling the solution done.
Extra space
One too many
Space loop must print exactly i - 1 spaces (range(i - 1)). Using range(i) breaks the right edge.
Program 3 formulas
Grows instead
rows - i spaces and 1..i stars is Program 3. Here use i - 1 and rows - i + 1.
No spaces
Left-aligned invert
Skipping the space loop gives Program 2. Right alignment needs leading spaces.
rows = 1
Single star
0 spaces + 1 star — same tip case as the other triangle pages.
rows ≤ 0
Empty output
Outer loop never runs. Validate and re-prompt for interactive programs.
Bad input
Catch ValueError
int(input()) throws on letters — prefer try / except ValueError.
Analysis
Time and Space Complexity
Program
Time
Extra space
Nested loops (Examples 1–2)
O(rows²)
O(1)
String multiply shortcut (Example 3)
O(rows²)
O(rows) per temporary row string
Each of n rows prints Θ(n) characters (spaces + stars). Star count alone is still n(n+1)/2.
Remember
Key Takeaways
Formulas:i - 1 spaces and rows - i + 1 stars.
Fixed width: spaces + stars = rows on every line.
Break the row:print(..., end="") for spaces/stars; bare print() after both loops.
Complexity:O(n²) time; O(1) extra space for nested loops.
One line: print i - 1 spaces, then rows - i + 1 stars — inverted and flush right.
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.
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.
print(" ", end="") or print("*", end="") stays on the same line. print() ends the current line. Spaces and stars use end=""; the row break uses print() after both inner loops.
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.
Wrap int(input()) in try/except ValueError and require rows >= 1 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.