Increasing-Decreasing Palindrome Number Pattern in Python

What You’ll Learn
How to print a palindrome-style number pattern in Python. Each row begins at the row number, increases to a peak, and then decreases back to the start.
This is a great practice problem for building a row with two phases: increasing numbers and then decreasing numbers.
⭐ Pattern Output
For rows = 5, the pattern looks like this:
1
232
34543
4567654
567898765Complete Python Program
For each row i, print i..(2i-1) (increasing), then print back down to i (decreasing).
rows = 5
for i in range(1, rows + 1):
m = i
for _ in range(i):
print(m, end="")
m += 1
m -= 2
for _ in range(1, i):
print(m, end="")
m -= 1
print()🧠 How It Works
Choose the number of rows
rows = 5 controls the height of the pattern.
Outer loop picks the row start
For row i, we start from m = i.
First inner loop (increasing)
We print i values: i, i+1, ..., 2i-1 by incrementing m.
Second inner loop (decreasing)
We step back by 2 and print i-1 values descending to mirror the left side: 2i-2 down to i.
Palindrome-like row
Row i prints 2i-1 digits. Total output grows roughly with \(r^2\), so runtime is O(r²) for r rows.
Variation — User Input Version
Read the number of rows from the user and print the same pattern.
rows = int(input("Enter number of rows: "))
if rows < 1:
raise ValueError("rows must be at least 1")
for i in range(1, rows + 1):
m = i
for _ in range(i):
print(m, end="")
m += 1
m -= 2
for _ in range(1, i):
print(m, end="")
m -= 1
print()💡 Tips for Enhancement
Try These
- Add spaces between values for readability when rows get larger
- Center-align the pattern by printing leading spaces on each row
- Print the same idea with letters (A, B, C...) instead of digits
- Store the row in a list first, then join and print (alternative approach)
- Change the start value to print odd-only or even-only sequences
Avoid
- Forgetting to reset
mfor each row (it will break the pattern) - Skipping the
m -= 2step (the center number will repeat incorrectly) - Using
rows < 1without validation - Printing large multi-digit numbers without spacing (output becomes hard to read)
Key Takeaways
Row i starts at i and increases up to 2i-1.
The second loop mirrors the row by printing back down to i.
Each row contains exactly 2i-1 digits.
Total printed digits grow quadratically, so runtime is O(r²).
❓ Frequently Asked Questions
m is one step past the peak. Subtracting 2 sets it to the correct value to start the mirror without repeating the peak incorrectly.2i-1. For example, when i = 4, the row goes up to 7: 4567654.Explore More Python Number Patterns!
Palindrome-style rows help you build intuition for symmetric printing and two-phase loops.
The total digits printed up to r rows form a perfect square: 1+3+5+…+(2r-1)=r². That’s why these odd-length rows scale neatly.
7 people found this page helpful
