Inverted Repeated Number Triangle in Python

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:
11111
2222
333
44
5Complete 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.
rows = 5
for i in range(1, rows + 1):
for _ in range(i, rows + 1):
print(i, end="")
print()🧠 How It Works
Choose the row count
rows = 5 sets the length of the first row.
Outer loop (digit per row)
for i in range(1, rows + 1) chooses the digit: 1, 2, 3, 4, 5.
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.
New line
print() ends each row.
Inverted repeated 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, 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
itimes - 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
The outer loop selects the digit for each row (1 to rows).
The inner loop repeats the digit from i to rows, so rows shrink each line.
Total printed digits follow the triangular number count: n(n+1)/2.
This is an inverted version of repeated-number triangles.
❓ Frequently Asked Questions
i increases each row, but the inner loop runs from i to rows, so it executes fewer times.i times. That’s Program 11.print(i, end=" "). For alignment, consider fixed-width formatting.Explore More Python Number Patterns!
Keep going to discover more number pattern programs and strengthen your loop skills.
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.
12 people found this page helpful
