Shape Rule
Flush right
Same 1…n stars as Program 1, shifted right with leading spaces.

The right-aligned triangle keeps the same star counts as Program 1, but adds a leading-space loop so the right edge stays straight. This tutorial covers the space formula, two inner loops, a live preview, algorithm steps, worked Python examples, edge cases, and complexity.
Flush right
Same 1…n stars as Program 1, shifted right with leading spaces.
rows - i
Print rows - i spaces before the stars on row i.
i stars
Same as Program 1: print exactly i stars after the padding.
n chars/row
Every row has (rows - i) + i = rows characters before the newline.
1–20 rows
Pick a row count and draw the right-aligned triangle instantly.
Complexity
n rows × Θ(n) characters each — O(n²) time, O(1) extra space.
A right-aligned right-angled triangle grows by one star per row, but the right angle sits on the right edge. You get there by printing leading spaces before the stars.
Compared with Program 1, you keep the same i star counts and add a second inner loop for (rows - i) spaces. That is the usual stepping stone toward centered pyramids.
Leading spaces are how console patterns create alignment and centering. Once this space loop clicks, pyramids and diamonds reuse the same idea.
Print rows - i spaces, then stars.
Row i still has exactly i stars.
One for padding, one for stars.
Foundation for pyramids and diamonds.
In short: for each row i, print rows - i spaces, then i stars, then a newline — the right edge stays flush.
Given a positive integer rows, print a right-aligned right-angled triangle of * characters with rows lines.
# First 5 rows (spaces shown as ·)
# ····*
# ···**
# ··***
# ·****
# ***** | Item | Type | Description |
|---|---|---|
rows | int | Number of triangle lines (typically ≥ 1). Also the width of each line. |
| Printed output | text | Right-aligned rows: (rows - i) spaces + i stars. |
for i from 1 to rows:
for j from 1 to (rows - i):
print " " (no newline)
for k from 1 to i:
print "*" (no newline)
print newline | Approach | Idea | Best for |
|---|---|---|
| Two inner loops | Spaces then stars with separate counters | Learning and interviews |
"*" * shortcut | Build padding and stars as strings | Shorter demos after formulas click |
| Goal | Pattern |
|---|---|
| Walk each row | for i in range(1, rows + 1): |
| Leading spaces | for j in range(1, rows - i + 1): print(" ", end="") |
Print i stars | for k in range(1, i + 1): print("*", end="") |
| End the row | print() |
| Width check | (rows - i) + i == rows |
| String shortcut | print(" " * (rows - i) + "*" * i) |
Same family — alignment and direction change the picture.
left-alignedOnly i stars — no space loop
spaces + starsrows - i spaces, then i stars
inverted + rightGrowing spaces, shrinking stars
name both loopsSay padding formula before star formula
Reach for right alignment when teaching leading spaces after a left-aligned triangle.
Natural next lab: keep star counts, add a space loop.
Practice two different per-row formulas in one figure.
Centered pyramids reuse the same space formula idea.
Every line spans exactly rows columns — easy to verify.
Console-style teaching pattern — not how you build app screens.
Key benefit: one small space loop that unlocks alignment, centering, and most later star patterns.
Choose a row count between 1 and 20 and draw the right-aligned triangle in the browser.
Three complete Python programs — nested space/star loops, console input, and a "*" * shortcut. Click View Output to reveal sample console results.
Print five right-aligned rows with classic nested loops.
rows = 5Space loop first, then star loop — the standard textbook version.
rows = 5
for i in range(1, rows + 1):
for j in range(1, rows - i + 1):
print(" ", end="")
for k in range(1, i + 1):
print("*", end="")
print() When i = 1, print 4 spaces then 1 star. When i = 5, print 0 spaces then 5 stars. Every row has exactly 5 characters before the newline.
Let the user choose the height at runtime.
Read rows with input() and int() (wrap in try/except ValueError in real apps).
rows = int(input("Enter the number of rows: "))
for i in range(1, rows + 1):
for j in range(1, rows - i + 1):
print(" ", end="")
for k in range(1, i + 1):
print("*", end="")
print() 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.
Same shape without explicit character loops.
" " * and "*" * MultiplicationBuild each row’s padding and star run in one call each.
rows = 5
for i in range(1, rows + 1):
print(" " * (rows - i) + "*" * i) Same formulas as Example 1; "*" * replaces the two inner loops. Keep the nested-loop version for exams that want both bounds visible.
Set rows. Use i for the row, j for spaces, k for stars.
for i in range(1, rows + 1): walks from one star up to rows stars.
for j in range(1, rows - i + 1): print(" ", end="") shifts the star block right.
Print i stars with print("*", end=""), then print(). Row width is always rows.
Star total n(n+1)/2; O(n²) time, O(1) extra space.
rows = 4Trace spaces, stars, and total width for each outer-loop value of i.
i | Spaces rows - i | Stars i | Width | Printed row |
|---|---|---|---|---|
1 | 3 | 1 | 4 | * |
2 | 2 | 2 | 4 | ** |
3 | 1 | 3 | 4 | *** |
4 | 0 | 4 | 4 | **** |
Check: every row has width 4. Star total: 1+2+3+4 = 10.
Where leading-space alignment (and this triangle) shows up beyond the homework prompt.
Clearest intro to a second inner loop for spaces.
Example: remove the space loop and recover Program 1.
Centered pyramids reuse rows - i (or similar) padding.
Example: Program 5 adds odd star counts.
Assert every line has length rows while debugging.
Example: count printed chars before print().
Swap * for digits once alignment works.
Example: print i instead of *.
Program 4 combines right alignment with shrinking stars.
Example: flip space growth direction.
Pair with input validation and positive-row checks.
Example: reject rows <= 0 and re-prompt.
Pro Tip: in interviews, say “Program 1 star counts plus rows - i spaces” before writing loops.
Why this right-aligned pattern is a strong third exercise.
Same star loop — only one new concept (leading spaces).
Every row length equals rows — bugs show up immediately.
Pyramids and diamonds reuse the same padding idea.
Streaming output needs only loop counters.
Pro Tip: master the two-loop version first; treat "*" * as a polish shortcut afterward.
Small habits that keep right-aligned pattern code clean.
Always print padding first — order matters for alignment.
Tabs break alignment across fonts and editors.
int(input()) in try/exceptAvoid crashes when the user types letters instead of a number.
rowsMentally check (rows - i) + i on tip and last rows.
Trace rows = 3 or 4 on paper before coding larger demos.
Pro Tip: if the output looks left-aligned, you almost certainly forgot the space loop (or ran it with 0 iterations).
Mistakes that commonly break right-aligned star triangles.
You reprint Program 1 — left-aligned, not flush right.
→ Print rows - i spaces before the stars.
Using i spaces and rows - i stars breaks the right edge.
→ Spaces = rows - i; stars = i.
j < rows - i instead of j <= rows - i drops a needed space.
→ Use j <= rows - i for this formulation.
Alignment looks fine in one editor and broken in another.
→ Always print the space character " ".
Letters or empty input throw ValueError.
→ Catch ValueError and re-prompt on failure.
Check these inputs before calling the solution done.
0 spaces + 1 star — same as Program 1 for n = 1.
Outer loop never runs — print nothing or show a message.
rows < 0Treat as invalid; re-prompt instead of silent empty output.
Each line is n characters — fine for labs; may wrap on tiny terminals.
int(input()) raises ValueError — validate first.
i == rowsSpace loop runs 0 times — only stars, flush left and right.
Try these variations to lock in the pattern.
rows >= 1*rows: (rows - i) + i = rows.rows > 0 for interactive programs; rows = 1 prints a single star.Quick Takeaway: print rows - i spaces, then i stars, then a newline — that is right alignment.
| Program | Time | Extra 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).
The right-aligned triangle is Program 1 plus a leading-space loop: rows - i spaces, then i stars. That single idea unlocks alignment and most later console patterns.
Practice the three examples above, then continue to the inverted right-aligned triangle.
Spaces first, then stars — keep the width check (rows - i) + i == rows, and validate row counts when reading input.
rows - i spaces and i starsrowstry/except ValueError for interactive demosj < rows - i when you meant <=rows = 1 edge casePrint the flush-right triangle the beginner-friendly way.
Spaces then stars
Definitionrows - i
Formulai (same as P1)
FormulaAlways rows
CheckO(n²) time
AnalysisRight-aligned and left-aligned triangles use the same star counts per row; only leading spaces change. Each row prints exactly rows characters before the newline: (rows - i) + i = rows.
Keep the right edge flush while shrinking the star count each row.
12 people found this page helpful