Palindromic Number Pyramid in Python

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

What You’ll Learn

How to print a palindromic number pyramid in Python, where each row reads the same forward and backward:

  • 1
  • 121
  • 12321
  • 1234321
  • 123454321

The core idea is to print an increasing sequence, then print the decreasing sequence without repeating the peak.

⭐ Pattern Output

For 5 rows, the pattern looks like this:

Output
1
121
12321
1234321
123454321
1

Complete Python Program

The first inner loop prints ascending numbers, and the second prints descending numbers to form the palindrome.

Python
for i in range(2, 7):
    for j in range(1, i):
        print(j, end="")
    for k in range(i - 2, 0, -1):
        print(k, end="")
    print()

🧠 How It Works

1

Outer loop controls the height

for i in range(2, 7) generates 5 rows (i = 2..6). Each row prints up to i - 1.

Row control
2

Print ascending part

for j in range(1, i) prints 1 through i - 1.

Ascending
3

Print descending part

for k in range(i - 2, 0, -1) prints i - 2 down to 1, completing the palindrome without repeating the peak digit.

Descending
4

New line after each row

print() moves to the next row.

Line break
=

Palindromic rows

Each row has length \(2r-1\). Total digits printed across \(n\) rows is \(1+3+5+\dots+(2n-1)=n^2\), so overall time is O(n²).

2

Variation — User Input Version

Choose the number of rows at runtime. This keeps the same palindrome idea for any size.

Python
rows = int(input("Enter the number of rows: "))

for i in range(2, rows + 2):
    for j in range(1, i):
        print(j, end="")
    for k in range(i - 2, 0, -1):
        print(k, end="")
    print()

💡 Tips for Enhancement

Try These

  • Validate input (reject rows < 1) before printing
  • Right-align or center the pyramid using leading spaces
  • Add spaces between digits for readability (then update output)
  • Replace numbers with letters to create alphabet palindromes
  • Build the row as a string for faster printing in large patterns

Avoid

  • Repeating the peak digit in the second loop (it breaks the palindrome)
  • Forgetting print() after each row
  • Mixing default print behavior with end="" in the same row
  • Assuming user input is always valid (wrap int() conversion if needed)

Key Takeaways

1

Print ascending digits first, then descending digits to form each palindrome row.

2

Start the descending loop at i - 2 to avoid repeating the peak digit.

3

Row lengths are odd: 1, 3, 5, ... up to \(2n-1\).

4

Total digits printed across \(n\) rows is \(n^2\), so runtime is O(n²).

❓ Frequently Asked Questions

Starting at i = 2 makes the first row print only 1 (since the ascending loop prints 1..i-1). That cleanly matches the pattern output.
Print leading spaces before the ascending loop. For row r (starting at 1), print rows - r spaces first, then print the palindrome digits.
Yes. Instead of printing integers, print characters using chr(ord('A') + x) or similar. The ascending/descending structure remains the same.
O(n²) for \(n\) rows, because total digits printed are \(1+3+\dots+(2n-1)=n^2\).

Explore More Python Number Patterns!

Keep practicing symmetry and nested loops with more number pattern programs.

All Number Patterns →
Did you know?

Palindromic patterns are a great way to practice thinking in terms of “build left half, then mirror it”—the same idea used in many string and array problems.

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