Python Increasing Number Triangle Pattern (Right-Aligned)

Beginner
5 min read
Updated: Sep 2026
3 programs
Live preview

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.

How to Solve It

One outer row loop, a space loop for indent, then an ascending number loop with :2d.

MethodIdeaBest for
Right-alignedSpaces while j > i, then :2d for 1..iLearning, interviews, exams
Rows inputSame logic with a user-chosen rowsPractice / 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

GoalPattern
Walk rowsfor i in range(1, rows + 1):
Leading spacesfor j in range(rows, i, -1): print(" ", end="")
Print 1..ifor k in range(1, i + 1): print(f"{k:2d}", end="")
End the rowprint()

Printing Numbers vs Starting a New Line

APIEffectUse for
print(" ", end="") / print(f"{k:2d}", end="")Stays on the same lineEach space or number
print()Ends the current lineAfter both inner loops

Print spaces and numbers without a newline, then end the row once.

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 result 5 rows · 15 numbers
     1
    1 2
   1 2 3
  1 2 3 4
 1 2 3 4 5

Worked Walkthrough — rows = 4

Trace spaces and the ascending sequence on each row.

iSpaces / numbersPrinted row
13 spaces / 11
22 spaces / 1 21 2
31 space / 1 2 31 2 3
4none / 1 2 3 41 2 3 4

Space count = rows − i. Total numbers for n rows = n(n+1)/2.

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()

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()

How It Works

1. Validate rows. Catch ValueError; require rows >= 1.

2. Same triangle. Only the bound changes from the literal 5 to the user value.

3. Safer input tip. Cap demos for readable output:

Safer input tip
if rows < 1 or rows > 9:
    print("Enter a whole number from 1 to 9.")
    raise SystemExit(1)

Example 3 — Left-Aligned Contrast

Skip the space loop — numbers start at the left margin.

Python
rows = 5

for i in range(1, rows + 1):
    for k in range(1, i + 1):
        print(f"{k:2d}", end="")

    print()

How It Works

1. No indent. Without the space loop, every row starts at column 0.

2. Same numbers. f"{k:2d}" still keeps digit columns stable.

3. Compare. Place this next to Example 1 to see what the space loop contributes.

Edge Cases & Pitfalls

Check these before calling the solution done.

no spaces

Forgot the indent loop

The triangle snaps left. Keep for j in range(rows, i, -1) for right alignment.

no :2d

Bare print(k, end="")

Columns drift once values hit two digits. Prefer f"{k:2d}".

print() inside

print() inside an inner loop

Each space or number lands on its own line. End the row only after both loops.

rows = 1

Single row

Output is just the formatted 1 — no leading spaces.

input()

Catch ValueError

Bare int(input()) crashes on non-numeric text — wrap it in try/except ValueError.

two spaces

Indenting with " "

This pattern uses one space per indent step plus :2d on numbers. Two-space indents over-shift the triangle.

Time and Space Complexity

ProgramTimeExtra space
Fixed / left-aligned (Examples 1, 3)O(n²)O(1)
User input (Example 2)O(n²)O(1)

Total printed numbers are 1 + 2 + … + n = n(n+1)/2, so work is quadratic in the row count.

Key Takeaways

  • Rule: spaces while j > i, then print 1..i with :2d.
  • Align: space count = rows − i; width-2 keeps digits in columns.
  • end="" vs print(): spaces and numbers stay on the line; bare print() advances after each row.
  • Complexity: O(n²) time; O(1) extra space.

One line: indent with spaces, then print 1 to i with fixed width on each row.

Frequently Asked Questions

A right-aligned ascending triangle: row 1 prints 1, row 2 prints 1 2, row 3 prints 1 2 3, and so on.
Before printing numbers, the program prints spaces while j > i. Smaller rows get more spaces, pushing digits toward the right edge.
It reserves 2 columns per number (right-aligned), so columns stay stable when values become two digits.
Remove the space loop that prints leading spaces, then print numbers 1 to i directly — see Example 3.
Program 42 prints a hollow square with border conditions. Program 43 prints an ascending number triangle with indentation spaces.
Use a rows variable and loop i from 1 to rows — see Example 2.
O(n²) for n rows because total prints are 1+2+…+n = n(n+1)/2.
Use try/except ValueError around int(input()) and require rows >= 1 — see Example 2.
Only one row prints — a single formatted 1 with no leading spaces.

Did you know?

Each row prints numbers 1 to i. A space loop runs while j > i before the number loop; f"{k:2d}" keeps columns aligned. Total prints = n(n+1)/2.

Next: Diamond Number Pyramid

Continue with a centered diamond of ascending digit sequences.

Program 44 tutorial →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

12 people found this page helpful