Repeated Number Triangle in Python

Beginner
⏱️ 5 min read
📚 Updated: Aug 2025
🎯 2 Code Examples
Nested Loops

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:

Output
1
22
333
4444
55555
1

Complete Python Program

The outer loop picks the digit for the row. The inner loop prints it i times.

Python
rows = 5

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

🧠 How It Works

1

Choose the row count

rows = 5 sets how many lines to print.

Setup
2

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.

Row control
3

Inner loop (repeat i times)

for _ in range(i) runs i times and prints i using print(i, end="").

Repetition
4

New line

print() moves to the next row.

Line break
=

Repeated number triangle

Total digits printed are 1+2+…+n = n(n+1)/2, so time complexity is O(n²) for n rows.

2

Variation — User Input Version

Let the user decide the number of rows at runtime using input():

Python
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 123 style 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

1

The outer loop selects the digit printed on that row.

2

The inner loop repeats the digit exactly i times.

3

Total printed digits follow the triangular number count: n(n+1)/2.

4

This is a common starter pattern for mastering nested loops.

❓ Frequently Asked Questions

Because the inner loop prints i repeatedly rather than printing j.
Yes. Loop i from rows down to 1 and repeat each digit accordingly (see Program 10).
Use print(i, end=" "). For consistent alignment, consider fixed-width formatting.
O(n²) for n rows: total prints are 1+2+…+n = n(n+1)/2.

Explore More Python Number Patterns!

Keep going to discover more number pattern programs and strengthen your loop skills.

All Number Patterns →
Did you know?

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²).

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