Increasing-Decreasing Palindrome Number Pattern in Python

Beginner
⏱️ 6 min read
📚 Updated: Aug 2025
🎯 2 Code Examples
Two Inner Loops

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:

Output
1
232
34543
4567654
567898765
1

Complete Python Program

For each row i, print i..(2i-1) (increasing), then print back down to i (decreasing).

Python
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

1

Choose the number of rows

rows = 5 controls the height of the pattern.

Setup
2

Outer loop picks the row start

For row i, we start from m = i.

Row control
3

First inner loop (increasing)

We print i values: i, i+1, ..., 2i-1 by incrementing m.

Up
4

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.

Down
=

Palindrome-like row

Row i prints 2i-1 digits. Total output grows roughly with \(r^2\), so runtime is O(r²) for r rows.

2

Variation — User Input Version

Read the number of rows from the user and print the same pattern.

Python
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 m for each row (it will break the pattern)
  • Skipping the m -= 2 step (the center number will repeat incorrectly)
  • Using rows < 1 without validation
  • Printing large multi-digit numbers without spacing (output becomes hard to read)

Key Takeaways

1

Row i starts at i and increases up to 2i-1.

2

The second loop mirrors the row by printing back down to i.

3

Each row contains exactly 2i-1 digits.

4

Total printed digits grow quadratically, so runtime is O(r²).

❓ Frequently Asked Questions

One loop builds the increasing left-to-center part, and the second loop builds the decreasing part to mirror it.
After the increasing loop, m is one step past the peak. Subtracting 2 sets it to the correct value to start the mirror without repeating the peak incorrectly.
The peak is 2i-1. For example, when i = 4, the row goes up to 7: 4567654.
O(r²), because the total digits printed across all rows scale like \(1+3+5+\dots+(2r-1)=r^2\).

Explore More Python Number Patterns!

Palindrome-style rows help you build intuition for symmetric printing and two-phase loops.

All Number Patterns →
Did you know?

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.

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.

7 people found this page helpful