Palindromic Number Pyramid in Python

Beginner
⏱️ 7 min read
📚 Updated: Aug 2025
🎯 2 Code Examples
Center Alignment

What You’ll Learn

How to print a palindromic number pyramid in Python. Each row prints increasing numbers from 1 to the row index, then decreasing back to 1.

You’ll also learn how leading spaces are used to center-align the pyramid.

⭐ Pattern Output

For rows = 5, the pattern looks like this:

Output
      1
    1 2 1
  1 2 3 2 1
1 2 3 4 3 2 1
1 2 3 4 5 4 3 2 1
1

Complete Python Program

Print spaces first, then print ascending numbers up to i, and finally print descending numbers back to 1.

Python
rows = 5

for i in range(1, rows + 1):
    for _ in range(rows, i, -1):
        print(" ", end=" ")
    for k in range(1, rows + 1):
        if k <= i:
            print(k, end=" ")
    for m in range(i - 1, 0, -1):
        print(m, end=" ")
    print()

🧠 How It Works

1

Set the pyramid height

rows = 5 sets how many rows the pyramid will have.

Setup
2

Print leading spaces

The first loop prints spaces so the numbers shift left as i increases, giving a centered pyramid.

Alignment
3

Ascending sequence (1..i)

The second loop prints numbers from 1 up to the current row limit i.

Up
4

Descending sequence (i-1..1)

The third loop prints i-1 down to 1 to mirror the left side and form a palindrome.

Down
=

Symmetric pyramid

Each row prints \(2i-1\) numbers, so total output is \(O(r^2)\) for r rows.

2

Variation — User Input Version

Let the user choose how many rows to print.

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):
    for _ in range(rows, i, -1):
        print(" ", end=" ")
    for k in range(1, rows + 1):
        if k <= i:
            print(k, end=" ")
    for m in range(i - 1, 0, -1):
        print(m, end=" ")
    print()

💡 Tips for Enhancement

Try These

  • Use a fixed-width cell for better alignment with multi-digit numbers
  • Print without spaces for a compact pyramid
  • Build each row in a list and join it to avoid trailing spaces
  • Invert the pyramid by printing rows in reverse order
  • Replace numbers with letters for an alphabet palindrome pyramid

Avoid

  • Forgetting the descending loop (the row won’t be palindromic)
  • Printing incorrect spaces (the pyramid won’t align)
  • Mixing 0-based and 1-based ranges in the loop boundaries
  • Using rows < 1 without validation

Key Takeaways

1

Leading spaces align the pyramid to the center.

2

Each row prints 1..i then i-1..1.

3

Each row contains 2i-1 numbers.

4

Total printed values grow as O(r²).

❓ Frequently Asked Questions

Printing i-1 down to 1 mirrors the ascending part without repeating the peak value i, making the row palindromic.
Row i prints i ascending numbers and i-1 descending numbers, so it prints 2i-1 numbers in total.
Because we also print numbers with a trailing space, using two spaces for indentation helps keep the columns aligned.
O(r²), because total printed numbers across r rows are 1+3+5+…+(2r-1)=r².

Explore More Python Number Patterns!

Palindrome pyramids are a fun way to practice symmetry and loop boundaries.

All Number Patterns →
Did you know?

These rows use an odd count of numbers: 1, 3, 5, 7… and the sum of the first \(r\) odd numbers is \(r^2\).

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