Python Number Triangle Pattern (Repeated Digits)

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

What Is This Pattern?

A repeating number triangle prints the row index i exactly i times on each row — 1, 22, 333, 4444, and so on.

Remember
Rule: for i from 1 to rows
        for j from 1 to i: print i (no space)

1
22
333
4444
55555     ← rows = 5

Follows the reverse left-growing triangle in Program 8; next is the descending repeating triangle in Program 10.

How to Solve It

Outer loop picks the digit i. Inner loop runs i times and prints i each time — not j.

MethodIdeaBest for
Print i in the inner loopOuter i = 1..rows; inner j = 1..i; print iLearning, interviews, exams
Spaced digitsprint(str(i) + " ", end="") instead of print(i, end="")Easier reading on each row

Pseudocode

Pseudocode
for i from 1 to rows:
    for j from 1 to i:
        print i
    print newline

Cheat sheet

GoalPattern
Pick each rowfor i in range(1, rows + 1):
Repeat digitfor j in range(1, i + 1): print(i, end="")
End of rowprint()
Spaced formprint(str(i) + " ", end="")
vs Program 55 prints j; 9 prints i — same loop bounds

Printing Numbers vs Starting a New Line

APIEffectUse for
print(i, end="")Stays on the same lineEach repeated digit on the row
print()Ends the current lineAfter the inner loop finishes

Digits use end=""; a bare print() ends the row once.

Live Preview

Change the row count and the repeating triangle updates instantly — capped at 9 for readable single-digit demos.

Whole numbers from 1 to 9. Tap a chip or type a value — the preview redraws as you go.

Live result rows = 5 · 15 digits
1
22
333
4444
55555

Worked Walkthrough — rows = 5

Trace how the inner loop count equals the digit being printed.

iInner runsPrints
11 time1
22 times22
33 times333
55 times55555

Total digits = n(n+1)/2 → O(n²). The key mistake to avoid: printing j instead of i.

Python Programs

Three complete programs: fixed rows = 5, user-input rows, and a compact rows = 3 demo. Use View Output to reveal sample results.

Example 1 — Fixed rows = 5

Hard-coded height — print i inside the inner loop, not j.

Python
rows = 5

for i in range(1, rows + 1):
    for j in range(1, i + 1):
        print(i, end="")
    print()

How It Works

1. Outer picks the digit. i is both the row index and the value to repeat.

2. Inner repeats it. j only controls how many times — it is never printed.

3. Newline. Call bare print() only after the inner loop finishes the row.

Example 2 — User Input Rows

Read rows safely, validate, then run the same nested loops.

Python
try:
    rows = int(input("Enter the number of rows: "))
except ValueError:
    print("Please enter a positive integer.")
    raise SystemExit

if rows <= 0:
    print("Please enter a positive integer.")
    raise SystemExit

for i in range(1, rows + 1):
    for j in range(1, i + 1):
        print(i, end="")
    print()

How It Works

1. Prompt and validate. Catch ValueError; reject non-positive values before looping.

2. Same core. Outer i / inner print-i matches Example 1 — only rows comes from the user.

3. Safer input tip. Cap demos for readable single-digit output:

Safer input
try:
    rows = int(input("Enter the number of rows: "))
except ValueError:
    print("Enter a whole number from 1 to 9.")
    raise SystemExit

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

Example 3 — Compact rows = 3

Same structure with only three rows — easy to confirm you print i, not j.

Python
rows = 3

for i in range(1, rows + 1):
    for j in range(1, i + 1):
        print(i, end="")
    print()

How It Works

1. Three rows. i = 1 → 1; i = 2 → 22; i = 3 → 333.

2. Trace on paper. If you accidentally print j, row 3 becomes 123 (Program 5) — not 333.

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.

print(j)

Got Program 5 instead

If you print j instead of i, you get 1, 12, 123… Always print the outer variable.

i = rows..1

Descending repeats

Counting the outer loop down gives Program 10’s shape (5, 44, 333…). Keep outer 1..rows here.

print inside

Broken rows

If bare print() sits inside the inner loop, each digit lands on its own line. Call it only after the row finishes.

rows = 1

Single digit

Output is just 1 — a good sanity check for input validation.

rows > 9

Multi-digit width

Digits 10+ make tight rows hard to read. Cap demos at 9 or switch to spaced print(str(i) + " ", end="").

ValueError

Validate input

Use try/except ValueError and require rows > 0 before looping — bad input should not crash.

Time and Space Complexity

ProgramTimeExtra space
Fixed / input (Examples 1–2)O(n²)O(1)
Compact rows = 3 (Example 3)O(n²)O(1)

Total digits printed = 1 + 2 + … + n = n(n+1)/2 → O(n²) time. Only a few loop variables are needed.

Key Takeaways

  • Print i, not j: outer picks the digit; inner only counts repeats.
  • Same bound as Program 5: j = 1..i, but the printed value is i.
  • Break the row: print(i, end="") in the inner loop; bare print() after it.
  • Complexity: O(n²) time from n(n+1)/2 digits; O(1) extra space.

One line: for each i, print i exactly i times, then newline.

Frequently Asked Questions

A repeating number triangle: row 1 shows 1, row 2 shows 22, row 3 shows 333, and so on until row n shows the digit n repeated n times.
The inner loop prints the current row number i each time. It runs i times, so i is repeated i times on that row.
When i = 4, the inner loop runs 4 times and prints 4 each time.
Program 8 prints descending digits rows..i (5, 54, 543). Program 9 repeats the row digit i times (1, 22, 333).
Program 5 prints 1..i ascending per row. Program 9 prints i repeated i times — same inner bound, different print value.
print(i, end="") stays on the same line. print() ends the row after the inner loop.
Change rows or read it from user input with int(input()) inside try/except — see Example 2.
O(n²) for n rows because total prints are 1 + 2 + ... + n = n(n+1)/2.

Did you know?

Each row repeats the row number i exactly i times — outer i runs 1..rows, inner j prints i on every iteration — producing 1, 22, 333, and so on. Total prints grow as O(n²).

Next: Descending Repeating Triangle

Continue with rows that repeat a descending digit (5, 44, 333).

Program 10 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