Shape Rule
Repeat i, i times
Row with outer i = 3 prints 333 — the digit equals the row number, repeated that many times.

Program 9 prints a repeating number triangle: each row repeats the row digit i exactly i times — 1, 22, 333, and so on. This tutorial covers the shape rule, outer loop for the digit, inner loop for repetition, a live preview, worked Python examples, edge cases, and complexity.
Repeat i, i times
Row with outer i = 3 prints 333 — the digit equals the row number, repeated that many times.
i = 1..rows
for i in range(1, rows + 1) — chooses which digit to repeat on each row.
j = 1..i
for j in range(1, i + 1): print(i, end="") — repeats the digit i times.
Same line / next line
Digits use print(i, end=""); end each row with print().
rows = 3..9
Pick row count and draw the repeating triangle in the browser.
Complexity
Total prints = 1+2+…+n = n(n+1)/2 — a triangular number.
A repeating number triangle prints the row digit multiple times: row 1 prints 1 once, row 2 prints 2 twice, row 3 prints 3 three times, and so on. With rows = 5, you get 1, 22, 333, 4444, 55555.
In Python use an outer loop from 1 to rows, an inner loop that runs i times printing i each time, then print() after each row.
It teaches that the inner loop controls repetition count while the outer loop picks the value — a stepping stone to repeating stars and alphabets.
i is both row number and print value.
Inner runs i times, prints i.
Program 8 prints descending sequences; Program 9 repeats one digit.
Follow Program 8; continue to Program 10 next.
In short: outer i = 1..rows, inner j = 1..i, print(i, end="") each time, then print().
Given row count rows = 5, print a repeating number triangle — row i shows digit i repeated i times.
# rows = 5
# 1
# 22
# 333
# 4444
# 55555 | Item | Type | Description |
|---|---|---|
rows | int | Triangle height — also the widest row digit count. |
i (outer) | int | Current row digit — runs 1 up to rows. |
j (inner) | int | Repetition counter — runs 1..i, prints i each time. |
| Row width | int | Row with outer i prints exactly i copies of digit i. |
| First row | int | Single digit 1 when i = 1. |
| Last row | string | Digit rows repeated rows times. |
for i from 1 to rows:
repeat i times:
print i
print newline | Approach | Idea | Best for |
|---|---|---|
| Nested loops | for j = 1..i print i | Standard teaching approach |
| Descending outer | for i in range(rows, 0, -1) | Mirror triangle variant |
| User-input rows | int(input()) | Flexible height |
| Compact trace | rows = 3 on paper first | Quick dry-runs |
| Spaced output | print(i, end=" ") | Readable columns |
| Goal | Pattern |
|---|---|
| Outer loop | for i in range(1, rows + 1) |
| Inner loop | for j in range(1, i + 1): print(i, end="") |
| End row | print() |
| Program 5 contrast | Program 5: print j (1..i); Program 9: print i (repeat i times) |
Same repeating triangle — three ways to set row count and trace the logic.
rows = 5Hard-coded height for demos
int(input())Read row count from console
rows = 3Quick dry-run on paper
i = 1..rowsChooses digit to repeat
j = 1..iRepetition count
Reach for this pattern when teaching inner-loop repetition and comparing print value vs loop counter.
Simpler than descending sequences — one digit repeated per row builds repetition intuition.
Same inner-loop repetition idea extends to repeating * or letters.
Classic nested-loop question — explain outer picks value, inner controls count.
Compare this ascending repeat with the descending repeat in Program 10.
This is a console teaching pattern — not how you build modern app screens.
Key benefit: one small program that locks in value-vs-counter thinking and O(n²) repetition.
Choose a row count between 3 and 9 and draw the repeating number triangle in the browser.
Three complete Python programs — fixed rows, user input, and a compact trace with rows = 3. Click View Output to reveal sample console results.
Print five rows of the repeating number triangle with nested loops.
rows = 5Hard-coded height — outer picks digit, inner repeats it i times.
rows = 5
for i in range(1, rows + 1):
for j in range(1, i + 1):
print(i, end="")
print() Outer i runs 1 to 5 — inner j runs 1 to i, printing i each time.
Read row count from the user with validation.
Configurable height with int(input()) and a positive-rows check.
try:
rows = int(input("Enter the number of rows: "))
if rows <= 0:
raise ValueError
except ValueError:
print("Please enter a positive integer.")
else:
for i in range(1, rows + 1):
for j in range(1, i + 1):
print(i, end="")
print() Same nested loops — only the row count comes from console input with safe parsing.
Use rows = 3 for a quick paper trace before larger triangles.
rows = 3 TraceSmall triangle — easy to dry-run on paper before scaling up.
rows = 3
for i in range(1, rows + 1):
for j in range(1, i + 1):
print(i, end="")
print() Three rows, six total digits — trace i and inner count on paper before coding rows = 5.
rows = 5 sets how many rows to print.
for i in range(1, rows + 1) picks which digit to repeat on each row.
for j in range(1, i + 1) prints i exactly i times per row.
print() moves to the next row after each line is printed.
Total printed digits follow triangular numbers: n(n+1)/2, so time complexity is O(n²).
rows = 5Trace each row — outer i is the digit, inner j counts repetitions from 1 to i.
| Row (i) | Inner runs | Output line |
|---|---|---|
| 1 | 1 time | 1 |
| 2 | 2 times | 22 |
| 3 | 3 times | 333 |
| 4 | 4 times | 4444 |
| 5 | 5 times | 55555 |
Total digits printed: 1+2+3+4+5 = 15 = 5×6/2 — the fifth triangular number.
Where this repetition pattern shows up beyond the homework prompt.
Inner loop count equals print value — clearest introduction to repeat-N-times logic.
Example: trace row 3 and watch i print three times.
Program 8 prints descending sequences; Program 9 repeats one digit — same O(n²) total.
Example: compare output side by side for rows = 5.
Same inner-loop repetition extends to printing * or letters per row.
Example: replace print(i, end="") with print("*", end="").
Same inner bound 1..i — Program 5 prints j, Program 9 prints i.
Example: swap print value and see 123 vs 111 on row 3.
Triangular totals make O(n²) concrete for beginners.
Example: count printed digits for n = 10 → 55.
Pair the pattern with int(input()) and positive-row checks.
Example: reject rows <= 0 and re-prompt.
Pro Tip: when an interviewer asks for patterns, explain outer picks value and inner controls count first — then write the loops.
Why this pattern earns a permanent spot in beginner Python courses.
One digit repeated — wrong print value shows up immediately.
Only loops and console output — no arrays or math libraries.
Swap digits for stars, letters, or spaced output with one-line edits.
Streaming output needs no storage beyond loop counters.
Pro Tip: trace rows = 3 on paper — row 2 should print exactly two twos.
Small habits that keep repeating-triangle code clean.
print(i, end="") repeats the row digit — printing j gives 123 instead of 333.
Wrap int(input()) in try/except ValueError when the user might type letters.
Only call print() after the inner loop finishes the row.
for j in range(1, i + 1) — row i gets exactly i prints.
Trace rows = 3 on paper before coding larger demos.
Pro Tip: if row 3 shows 123 instead of 333, you are printing j instead of i.
Mistakes that commonly break repeating number triangles.
Row 3 prints 123 instead of 333 — classic value-vs-counter mix-up.
→ Use print(i, end="") inside the inner loop.
All digits print on one long line.
→ Call print() after the inner loop.
Inner running to rows instead of i over-prints each row.
→ Inner loop must be j = 1..i, not 1..rows.
Letters or empty input raise ValueError when int(input()) is unchecked.
→ Use try/except ValueError and re-prompt on failure.
Each digit prints on its own line — vertical output instead of a triangle.
→ Use print(i, end="") inside, print() outside only.
Check these inputs before calling the solution done.
Prints only 1 — inner loop runs once.
Outer loop never runs — print nothing or show a message.
Output 1 then 22 — good quick test.
Reject with validation before the loops.
Bare int(input()) raises ValueError — use try/except.
Still O(n²) prints — cap rows for console demos.
Try these variations to lock in the pattern.
print(i, end="") with print("*", end="")rows = 3 before codingi prints digit i exactly i times.n(n+1)/2 — triangular number. For rows = 5, that is 15 digits.1..i — only the print value differs.Quick Takeaway: outer i = 1..rows, inner j = 1..i, print(i, end=""), then print().
| Program | Time | Extra space |
|---|---|---|
| Nested loops (Examples 1–2) | O(rows²) | O(1) |
| Compact trace (Example 3) | O(rows²) | O(1) |
The repeating number triangle is one of the simplest nested-loop exercises: outer picks the digit, inner controls how many times it prints. Master the fixed rows = 5 version, then try user input and the compact rows = 3 trace.
Practice the three examples above, then continue to Program 10 for the descending repeating variant.
Print i not j, keep print() outside the inner loop, and validate row count when reading from the console.
for j in range(1, i + 1): print(i, end="")print() after each inner looprows > 0 for user inputrows = 3 on paper firstj when the pattern needs irows instead of iprint() inside the inner loopPrint the repeating number triangle the beginner-friendly way.
Repeat i, i times
Definitioni = 1..rows
Loopprint(i, end="") not j
Valuej = 1..i count
RepeatO(n²) time
AnalysisEach 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²).
Move on to the descending repeating triangle in the Python number-pattern series.
11 people found this page helpful