Inverted Repeated Number Triangle in Python

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

What You’ll Learn

How to print an inverted repeated number triangle in Python using nested for loops. Each row prints a digit repeatedly, but the row length decreases: 11111, 2222, 333, 44, 5.

This pattern is a great practice for using an increasing outer loop while making the inner loop run fewer times on each next row.

⭐ Pattern Output

For rows = 5, the pattern looks like this:

Output
11111
2222
333
44
5
1

Complete Python Program

The outer loop selects the digit for the row. The inner loop repeats it from i to rows, so each next row is shorter.

Python
rows = 5

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

🧠 How It Works

1

Choose the row count

rows = 5 sets the length of the first row.

Setup
2

Outer loop (digit per row)

for i in range(1, rows + 1) chooses the digit: 1, 2, 3, 4, 5.

Row control
3

Inner loop (repeat shrinks)

for _ in range(i, rows + 1) runs fewer times as i increases. That’s why the row lengths go 5, 4, 3, 2, 1.

Repetition
4

New line

print() ends each row.

Line break
=

Inverted repeated 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, rows + 1):
        print(i, end="")
    print()

💡 Tips for Enhancement

Try These

  • Validate input (reject rows < 1) before printing the pattern
  • Add spaces between digits with print(i, end=" ")
  • Print the non-inverted repeated triangle (Program 9) by repeating i times
  • Right-align the triangle by printing leading spaces before each row
  • Replace numbers with letters to make repeated-letter patterns

Avoid

  • Forgetting print() between rows
  • Mixing up loop bounds (remember the stop value is exclusive)
  • Using negative or zero rows without validation
  • Assuming user input is always valid (wrap int() conversion if needed)

Key Takeaways

1

The outer loop selects the digit for each row (1 to rows).

2

The inner loop repeats the digit from i to rows, so rows shrink each line.

3

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

4

This is an inverted version of repeated-number triangles.

❓ Frequently Asked Questions

Because i increases each row, but the inner loop runs from i to rows, so it executes fewer times.
Use a descending outer loop and repeat i times. That’s Program 11.
Yes. Use print(i, end=" "). For 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?

The total printed digits in these inverted triangles still form a triangular number: 1+2+…+n = n(n+1)/2. That’s why they typically have O(n²) output size.

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