Python Increasing Number Triangle Pattern (Right-Aligned)
Beginner
5 min read
Updated: Sep 2026
3 programs
Live preview
Definition
What Is This Pattern?
A right-aligned number triangle prints 1 to i on row i, with leading spaces so shorter rows sit against the right edge.
Remember
Rule: spaces while j > i, then print 1..i with width 2
1
1 2
1 2 3
1 2 3 4
1 2 3 4 5 ← rows = 5
Follows the hollow square border in Program 42; next is the diamond number pyramid in Program 44.
Approach
How to Solve It
One outer row loop, a space loop for indent, then an ascending number loop with :2d.
Method
Idea
Best for
Right-aligned
Spaces while j > i, then :2d for 1..i
Learning, interviews, exams
Rows input
Same logic with a user-chosen rows
Practice / demos
Pseudocode
Pseudocode
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 (width 2)
print newline
Cheat sheet
Goal
Pattern
Walk rows
for i in range(1, rows + 1):
Leading spaces
for j in range(rows, i, -1): print(" ", end="")
Print 1..i
for k in range(1, i + 1): print(f"{k:2d}", end="")
End the row
print()
Printing Numbers vs Starting a New Line
API
Effect
Use for
print(" ", end="") / print(f"{k:2d}", end="")
Stays on the same line
Each space or number
print()
Ends the current line
After both inner loops
Print spaces and numbers without a newline, then end the row once.
Try it
Live Preview
Change the row count and the right-aligned triangle updates instantly.
Whole numbers from 1 to 9. Tap a chip or type a value — the preview redraws as you go.
Live result5 rows · 15 numbers
1
1 2
1 2 3
1 2 3 4
1 2 3 4 5
Trace
Worked Walkthrough — rows = 4
Trace spaces and the ascending sequence on each row.
i
Spaces / numbers
Printed row
1
3 spaces / 1
1
2
2 spaces / 1 2
1 2
3
1 space / 1 2 3
1 2 3
4
none / 1 2 3 4
1 2 3 4
Space count = rows − i. Total numbers for n rows = n(n+1)/2.
Code
Python Programs
Three complete programs: fixed 5 rows, input() rows, and a left-aligned contrast. Use View Output for sample results.
Example 1 — Fixed rows = 5
Space loop for indent, then print 1..i with width 2.
Python
rows = 5
for i in range(1, rows + 1):
for j in range(rows, i, -1):
print(" ", end="")
for k in range(1, i + 1):
print(f"{k:2d}", end="")
print()
Output
1
1 2
1 2 3
1 2 3 4
1 2 3 4 5
How It Works
1. Outer loop.i runs from 1 to 5 — one row per value of i.
2. Indent. Print one space while j runs from rows down past i.
3. Numbers. Print k from 1 to i with f"{k:2d}", then end the line.
Example 2 — User Input Rows
Read the row count with input() and build the same right-aligned triangle.
Python
try:
rows = int(input("Enter number of rows: "))
except ValueError:
print("Please enter a positive whole number.")
raise SystemExit(1)
if rows < 1:
print("Please enter a positive whole number.")
raise SystemExit(1)
for i in range(1, rows + 1):
for j in range(rows, i, -1):
print(" ", end="")
for k in range(1, i + 1):
print(f"{k:2d}", end="")
print()