Python Incremental Number Triangle Pattern (Right-Aligned)
Beginner
5 min read
Updated: Sep 2026
3 programs
Live preview
Definition
What Is This Pattern?
A right-aligned continuous counter triangle prints a growing sequence with counter k that never resets — leading pads while j > i, then print(f"{k:3d}", end="").
Remember
Rule: k = 1
for i from 1 to rows
for j from rows down to 1
if j > i: print 3 spaces
else: print k (width 3); k++
1
2 3
4 5 6
7 8 9 10
11 12 13 14 15 ← rows = 5
Follows the zero-based triangle in Program 34; next is the right-aligned decreasing triangle in Program 36.
Approach
How to Solve It
Outer loop grows i. One fixed-width inner loop runs rows..1 — print a 3-space pad or the next k with f"{k:3d}".
Method
Idea
Best for
Fixed width + k += 1
Spaces while j > i; else print and increment k
Learning, interviews, exams
:3d columns
Keeps two-digit values aligned with single digits
Readable demos past 9
Pseudocode
Pseudocode
k = 1
for i from 1 to rows:
for j from rows down to 1:
if j > i: print 3 spaces
else: print k in width 3; k = k + 1
print newline
Cheat sheet
Goal
Pattern
Grow each row
for i in range(1, rows + 1):
Fixed width
for j in range(rows, 0, -1):
Leading pads
print(" ", end="")
Next number
print(f"{k:3d}", end=""); k += 1
End of row
print()
Printing Numbers vs Starting a New Line
API
Effect
Use for
print(" ", end="") / print(f"{k:3d}", end="")
Stays on the same line
Each pad or number on the row
print()
Ends the current line
After the fixed-width loop finishes
Print characters without a newline, then end the row once.
Try it
Live Preview
Change the row count and the right-aligned counter updates instantly — capped at 7 for readable demos.
Whole numbers from 1 to 7. Tap a chip or type a value — the preview redraws as you go.
Live resultrows = 5 · 15 numbers
1
2 3
4 5 6
7 8 9 10
11 12 13 14 15
Trace
Worked Walkthrough — rows = 5
Trace how leading pads shrink and counter k keeps climbing.
Three complete programs: fixed rows = 5, input() variant, and a compact rows = 3 demo. Use View Output to reveal sample results.
Example 1 — Fixed rows = 5
Hard-coded height — continuous k with f"{k:3d}" and leading pads.
Python
k = 1
for i in range(1, 6):
for j in range(5, 0, -1):
if j > i:
print(" ", end="")
else:
print(f"{k:3d}", end="")
k += 1
print()
Output
1
2 3
4 5 6
7 8 9 10
11 12 13 14 15
How It Works
1. Counter lives outside.k starts at 1 and only advances when a number prints.
2. Fixed width. Inner loop always runs 5 times — unused left slots become " ".
3. Width 3 columns.f"{k:3d}" keeps 10 and 11 aligned with single digits.
Example 2 — User Input Rows
Read rows with input(), validate, then use the same counter and pad logic.
Python
try:
rows = int(input("Enter rows: "))
except ValueError:
print("Please enter a positive integer.")
raise SystemExit(1)
if rows <= 0:
print("Please enter a positive integer.")
raise SystemExit(1)
k = 1
for i in range(1, rows + 1):
for j in range(rows, 0, -1):
if j > i:
print(" ", end="")
else:
print(f"{k:3d}", end="")
k += 1
print()
Output (when user enters 3)
Enter rows: 3
1
2 3
4 5 6
How It Works
1. Prompt and validate. Catch ValueError; reject non-positive values before printing.
2. Same core. Pads + f"{k:3d}" match Example 1 — only rows comes from the user.
3. Safer input tip. Cap demos for readable output:
Safer input tip
if rows < 1 or rows > 7:
print("Enter a whole number from 1 to 7.")
raise SystemExit(1)
Example 3 — Compact rows = 3
Same structure with only three rows — easy to confirm pads shrink and k never resets.
Python
rows = 3
k = 1
for i in range(1, rows + 1):
for j in range(rows, 0, -1):
if j > i:
print(" ", end="")
else:
print(f"{k:3d}", end="")
k += 1
print()
Output
1
2 3
4 5 6
How It Works
1. Three rows. Ends at 6 — if k reset each row you would see 1, 1 2, 1 2 3.
2. Trace on paper. If the inner loop stops at i instead of rows, right alignment is lost.
3. Scale up next. Once the small demo is clear, use Examples 1–2 for five rows or user input.
Edge Cases & Pitfalls
Check these before calling the solution done.
k = 1
Resetting the counter
Putting k = 1 inside the outer loop restarts every row. Declare k once before both loops.
no :3d
Broken columns
Without f"{k:3d}", two-digit values misalign with single digits. Keep fixed width.
width ≠ 3
Pad mismatch
Leading pads must match number width — use " " (3 spaces) with :3d.
print() inside
Broken rows
If bare print() sits inside the inner loop, you get one cell per line. Call it only after the loop.
rows = 1
Single value
Output is just 1 with no leading pads. A good sanity check for input validation.
input()
Catch ValueError
Bare int(input()) crashes on non-numeric text — wrap it in try/except ValueError.
Analysis
Time and Space Complexity
Program
Time
Extra space
Fixed / input (Examples 1–2)
O(n²)
O(1)
Compact rows = 3 (Example 3)
O(n²)
O(1)
Each of n rows visits n columns (pads or numbers). Numbers printed = n(n+1)/2 → O(n²) time. Only a few loop variables are needed.
Remember
Key Takeaways
Continuous k: declare once outside; use k += 1 only when printing a number.
Fixed width:j = rows..1 with pads when j > i keeps the triangle right-aligned.
:3d: match pad width to number width so two-digit values stay aligned.
Complexity:O(n²) time; O(1) extra space.
One line: for each row i, walk j = rows..1 and print a pad or f"{k:3d}" then k += 1, then bare print().
Frequently Asked Questions
A right-aligned continuous counter triangle: for rows=5 you get indented 1 / 2 3 / 4 5 6 / 7 8 9 10 / 11 12 13 14 15 — k never resets between rows.
Numbers keep increasing across rows without resetting — row 1 prints 1, row 2 prints 2 3, row 3 prints 4 5 6, and so on.
Before printing numbers on each row, the program prints three spaces while j > i. This indents the left side so numbers shift right.
The format specifier reserves 3 columns per number (right-aligned), keeping columns aligned when values become two digits.
k is declared outside the loops and increments with k += 1 each time a number prints, so the sequence continues across rows.
Program 30 prints descending digits per row. Program 35 uses a continuous counter k with fixed-width formatting.
Use try/except ValueError around int(input()) and require rows > 0 — see Example 2.
O(n²) for n rows because total prints are 1 + 2 + … + n = n(n+1)/2.
🤔
Did you know?
A counter k starts at 1 and increments every time a number is printed. Leading spaces appear while j > i, and print(f"{k:3d}", end="") keeps columns aligned as values grow past single digits.