Shape Rule
Right-aligned triangle
Row i prints numbers 1 to i, with leading spaces before the digits.

The right-aligned number triangle prints 1, then 1 2, then 1 2 3, … — a natural follow-up after Program 42’s hollow square border. This tutorial covers leading-space indentation, ascending sequences, nested loops, a live preview, worked Python examples, edge cases, and complexity.
Right-aligned triangle
Row i prints numbers 1 to i, with leading spaces before the digits.
i = 1..rows
for i in range(1, rows + 1): — ascending outer loop, one row per iteration.
rows..i+1
for _ in range(rows, i, -1): — prints a single space for right alignment.
end=" "
for k in range(1, i + 1): then print(k, end=" ").
3–9 rows
Pick a row count and draw the right-aligned number triangle in the browser.
Complexity
Total prints = n(n+1)/2 — work scales as n².
A right-aligned number triangle prints numbers from 1 to i on each row: 1, then 1 2, then 1 2 3, and so on. With rows = 5, shorter rows shift right thanks to a leading-space loop.
In Python you use three nested loops: print a space with range(rows, i, -1), then print(k, end=" ") for k = 1..i, then print().
It combines a space loop with an ascending number loop — a key step after Program 42’s hollow grid pattern.
Ascending sequence.
rows - i spaces.
Readable column spacing.
Follow Program 42; continue to Program 44 next.
In short: outer i = 1..rows, space loop range(rows, i, -1), numbers k = 1..i with end=" ", then print().
Given rows = 5, print a right-aligned ascending triangle: leading spaces while j > i, then numbers from 1 to i.
# rows = 5
# 1
# 1 2
# 1 2 3
# 1 2 3 4
#1 2 3 4 5 | Item | Type | Description |
|---|---|---|
rows | int | Triangle height — also controls leading-space count. |
i | int | Outer loop — current row number (1 to rows). |
j | int | Space loop — prints leading spaces while j > i. |
k | int | Number loop — prints digits 1..i. |
for i from 1 to rows:
for j from rows down to i+1: print one space
for k from 1 to i: print k with trailing space
print newline | Approach | Idea | Best for |
|---|---|---|
| Fixed rows | 1, 1 2, … | Learning and interviews |
| User-input rows | int(input(...)) | Configurable triangle size |
| Left-aligned variant | Remove space loop | Contrast with right alignment |
| Goal | Pattern |
|---|---|
| Outer loop | for i in range(1, rows + 1): |
| Space loop | for _ in range(rows, i, -1): print(" ", end="") |
| Number loop | for k in range(1, i + 1): |
| Print number | print(k, end=" ") |
| End the row | print() |
| Program 42 contrast | Hollow square grid — not an ascending triangle |
Same ascending triangle — different ways to control rows and alignment.
i = 1..rowsOne row per iteration
rows - iLeading-space indent
k = 1..iAscending sequence
skip space loopFlush-left triangle
Reach for this pattern when teaching dual inner loops, ascending sequences, and right-aligned console output.
Natural follow-up — moves from a 2D grid with border conditions to a triangle with leading spaces and ascending digits.
Practice separating space printing from number printing before tackling more complex shapes.
Combine loops with input() for flexible row counts.
Compare Program 42 (hollow square) and Program 44 (next in series) next.
This is a console teaching pattern — not how you build modern app screens.
Key benefit: one small program that locks in dual inner loops, formatted output, and O(n²) thinking.
Choose a row count between 3 and 9 and draw the right-aligned number triangle in the browser.
Three complete Python programs — fixed rows, user input, and left-aligned contrast. Click View Output to reveal sample console results.
Print five rows of the right-aligned number triangle with space and number loops.
rows = 5Hard-coded row count — ideal for first demos and screenshots.
rows = 5
for i in range(1, rows + 1):
for _ in range(rows, i, -1):
print(" ", end="")
for k in range(1, i + 1):
print(k, end=" ")
print() When i = 1, the space loop prints four spaces, then 1. When i = 5, no leading spaces — output 1 2 3 4 5.
Read the row count with input() instead of hard-coding 5.
Read rows with input() and validate rows > 0.
rows = int(input("Enter the number of rows: "))
if rows <= 0:
raise ValueError("rows must be positive")
for i in range(1, rows + 1):
print(" " * (rows - i), end="")
for k in range(1, i + 1):
print(k, end=" ")
print() Same space-and-number loop core as Example 1; only rows comes from user input instead of being hard-coded as 5.
Remove the space loop to see how right alignment changes the shape.
Same ascending sequence without leading spaces — numbers start flush left.
rows = 5
for i in range(1, rows + 1):
for k in range(1, i + 1):
print(k, end=" ")
print() Only the space loop is removed — the number loop stays the same. Compare this flush-left output with Example 1 to see what the space loop contributes.
No imports needed. Set rows = 5 and loop variables i, k.
for i in range(1, rows + 1): — ascending outer loop; one row per iteration.
for _ in range(rows, i, -1): — prints a single space for right alignment.
for k in range(1, i + 1): then print(k, end=" ") — ascending sequence.
print() ends the row after both inner loops finish.
Total numbers = n(n+1)/2 — O(n²) time, O(1) extra memory.
rows = 5, row i = 3Trace row 3 — space count, numbers printed, and full row output.
| Step | Detail | Output so far |
|---|---|---|
| Space loop | range(5, 3, -1) — two spaces | |
k = 1 | print(1, end=" ") | 1 |
k = 2 | print(2, end=" ") | 1 2 |
k = 3 | print(3, end=" ") | 1 2 3 |
| Newline | End row 3 | 1 2 3 |
Space count per row = rows - i. Numbers per row = i. Total prints = n(n+1)/2 for n rows.
Where this tiny pattern (and its loop structure) shows up beyond the homework prompt.
Clearest visual proof that outer and inner bounds interact.
Example: remove the space loop and watch the triangle snap left.
Foundation for right-aligned variants with separate space and number loops.
Example: compare with Program 42 (hollow square) and Program 44 next.
Practice end=" " spacing and row newlines without complex math.
Example: put print() inside the number loop by mistake.
Add leading spaces once the three-loop structure works.
Example: loop k from i down to 1 for a descending row variant.
Triangular totals make O(n²) concrete for beginners.
Example: count printed numbers for rows = 5 — total is 1+2+3+4+5 = 15.
Pair the pattern with input() checks and positive-row checks.
Example: reject rows <= 0 and re-prompt.
Pro Tip: when an interviewer asks for patterns, explain the outer/inner roles first — then write the loops. The story matters as much as the code.
Why this pattern earns a permanent spot in beginner Python courses.
Wrong bounds show up immediately as a broken staircase.
Only loops and console output — no arrays or math libraries.
Invert, center, hollow, or change the fill character with small edits.
Streaming output needs no storage beyond loop counters.
Pro Tip: trace i, the space loop, and the number loop on paper for rows = 3 before coding — watch how the space count shrinks each row.
Small habits that keep number-pattern code clean.
Space loop (range(rows, i, -1)) and number loop (k = 1..i) must run in order before print().
try/except ValueErrorUse try/except ValueError so bad input does not crash when converting rows.
Only call print() after the inner loop finishes the row.
Write the ascending sequence 1..i on paper before coding the number loop.
Trace i = 1..3 on paper before coding the full rows = 5 demo.
Pro Tip: if the output is a vertical list of single digits per line, you almost certainly put print() inside the inner loop.
Mistakes that commonly break right-aligned number triangles.
Each digit lands on its own line — you get a column, not a triangle.
→ Use print(k, end=" "); print() only after both inner loops.
Without for _ in range(rows, i, -1):, every row starts at the left margin.
→ Run the space loop before the number loop on every row.
Combining spaces and numbers in a single inner loop is harder to read and debug.
→ Keep separate space and number loops — see Examples 1 and 3.
Tab characters produce inconsistent alignment across consoles.
→ Use regular spaces or print(" " * (rows - i), end="").
int(input())Letters or empty input raise ValueError with bare int(input()).
→ Catch ValueError and re-prompt on failure.
Check these inputs before calling the solution done.
Output is just 1 on one line — no leading spaces when rows = 1.
Outer loop never runs when rows < 1 — print nothing or show a message.
rows < 1Treat as invalid; re-prompt instead of silent empty output.
Two rows: 1 and 1 2.
Bare int(input()) raises ValueError on bad input — use try/except first.
Total numbers = rows(rows+1)/2 — grows quadratically with rows.
Try these variations to lock in the pattern.
i prints 1 to irows - iint(input()) in try/except until rows >= 1i = 1..rows. Space loop range(rows, i, -1) prints one space. Number loop k = 1..i prints with end=" ".print(..., end=" ") stays on the line; print() advances — mix them carefully.rows ≥ 1 for interactive programs; rows = 1 prints a single 1.rows - i — compare with Example 3 where removing the space loop gives a left-aligned triangle.Quick Takeaway: outer i = 1..rows, space loop range(rows, i, -1), numbers k = 1..i with end=" ", then print().
| Program | Time | Extra space |
|---|---|---|
| Nested loops (Examples 1–3) | O(n²) | O(1) |
| Smaller demo (Example 3) | O(n²) | O(1) |
The right-aligned number triangle is a compact lesson in dual inner loops and formatted output: print spaces with range(rows, i, -1), print 1..i with end=" ", and end each row with print(). Master the fixed-rows version, then try user input and the left-aligned contrast.
Practice the three examples above, then continue to Program 44 for the next pattern in the series.
Leading spaces create right alignment — keep space and number loops separate and validate rows when reading input.
for i in range(1, rows + 1): in the outer loopfor _ in range(rows, i, -1): print(" ", end="")for k in range(1, i + 1): print(k, end=" ")rows ≥ 1 for interactive programsint(input()) in try/except ValueErrorprint() inside the number looprows = 1 edge casePrint the pattern the beginner-friendly way.
1..i per row
DefinitionRows i = 1..rows
Coderows - i spaces
Alignk = 1..i
CodeO(n²) time
AnalysisEach row prints numbers 1 to i. A space loop runs range(rows, i, -1) before the number loop; print(k, end=" ") keeps columns readable in the output.
Move on to the next pattern in the Python number-pattern series.
12 people found this page helpful