Repeated Number Triangle in Python

What You’ll Learn
How to print a repeated number triangle in Python using nested for loops. Each row prints the same digit multiple times: 1, 22, 333, 4444, and so on.
This pattern is a simple way to understand how the outer loop controls the row number while the inner loop controls repetition.
⭐ Pattern Output
For rows = 5, the pattern looks like this:
1
22
333
4444
55555Complete Python Program
The outer loop picks the digit for the row. The inner loop prints it i times.
rows = 5
for i in range(1, rows + 1):
for _ in range(i):
print(i, end="")
print()🧠 How It Works
Choose the row count
rows = 5 sets how many lines to print.
Outer loop (digit per row)
for i in range(1, rows + 1) selects the digit for each row: 1, then 2, then 3, and so on.
Inner loop (repeat i times)
for _ in range(i) runs i times and prints i using print(i, end="").
New line
print() moves to the next row.
Repeated number triangle
Total digits printed are 1+2+…+n = n(n+1)/2, so time complexity is O(n²) for n rows.
Variation — User Input Version
Let the user decide the number of rows at runtime using input():
rows = int(input("Enter the number of rows: "))
for i in range(1, rows + 1):
for _ in range(i):
print(i, end="")
print()💡 Tips for Enhancement
Try These
- Validate input (reject
rows < 1) before printing the pattern - Print with spaces using
print(i, end=" ")for readability - Swap digits and repetition to print
123style patterns instead - Right-align the triangle by printing leading spaces before each row
- Replace numbers with characters to create repeated-letter triangles
Avoid
- Forgetting
print()between rows - Using negative or zero rows without validation
- Mixing up the two loop roles (outer for rows, inner for repetition)
- Assuming user input is always valid (wrap
int()conversion if needed)
Key Takeaways
The outer loop selects the digit printed on that row.
The inner loop repeats the digit exactly i times.
Total printed digits follow the triangular number count: n(n+1)/2.
This is a common starter pattern for mastering nested loops.
❓ Frequently Asked Questions
i repeatedly rather than printing j.i from rows down to 1 and repeat each digit accordingly (see Program 10).print(i, end=" "). For consistent alignment, consider fixed-width formatting.Explore More Python Number Patterns!
Keep going to discover more number pattern programs and strengthen your loop skills.
This output has the same triangular digit count as many triangle patterns: 1+2+…+n = n(n+1)/2. That’s why nested-loop pattern printing often grows like O(n²).
12 people found this page helpful
