Python Palindromic Number Pyramid Pattern

Beginner
5 min read
Updated: Sep 2026
3 programs
Live preview

What Is This Pattern?

A palindromic number pyramid is a centered triangle where row i prints 1..i ascending, then i-1..1 descending — so every row reads the same forward and backward.

Remember
Rule: spaces (rows−i)×2, then 1..i, then i−1..1

        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     ← rows = 5

In Python print (rows - i) pairs of spaces with print(" ", end=""), then print(str(k) + " ", end="") ascending and descending, then a bare print().

How to Solve It

Per row: center with leading spaces, climb to the peak, then mirror down without repeating the peak.

MethodIdeaBest for
Three inner loopsSpaces, ascending 1..i, descending i-1..1Learning, interviews, exams
Rows inputSame logic with a user-chosen heightPractice / demos

Pseudocode

Pseudocode
for i from 1 to rows:
    print (rows - i) pairs of spaces
    for k from 1 to i:
        print k and a space
    for k from i - 1 down to 1:
        print k and a space
    print newline

Cheat sheet

GoalPattern
Outer loopfor i in range(1, rows + 1):
Leading spacesfor s in range(1, rows - i + 1): print(" ", end="")
Ascending halffor k in range(1, i + 1): print(str(k) + " ", end="")
Descending halffor k in range(i - 1, 0, -1): print(str(k) + " ", end="")
Digits on row i2 * i - 1

Printing Numbers vs Starting a New Line

APIEffectUse for
print(..., end="")Stays on the same lineSpaces and each digit plus trailing space
print()Ends the current lineAfter spaces + ascending + descending

Spaces and digits use end=""; a bare print() ends the row once.

Live Preview

Change the row count and the centered palindromic pyramid updates instantly — capped at 9 for readable single-digit demos.

Whole numbers from 1 to 9. Tap a chip or type a value — the preview redraws as you go.

Live result rows = 5 · bottom peak 5
        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 

Worked Walkthrough — Row i = 3 when rows = 5

Trace one middle row so centering and the skip-peak descent are clear.

StepLoopPrints
1s = 1..2 (rows - i) (4 spaces)
2k = 1..31 2 3
3k = 2..12 1

Full row: 1 2 3 2 1. Peak 3 appears once because descent starts at i - 1.

Python Programs

Three complete programs: fixed rows = 5, user-input rows, and a compact rows = 3 demo. Use View Output for sample results.

Example 1 — Fixed rows = 5

Hard-coded height — spaces, ascending, then descending on each row.

Python
rows = 5

for i in range(1, rows + 1):
    for s in range(1, rows - i + 1):
        print("  ", end="")

    for k in range(1, i + 1):
        print(str(k) + " ", end="")

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

    print()

How It Works

1. Center. Print (rows - i) pairs of spaces so upper rows sit further right.

2. Ascend. print(str(k) + " ", end="") from 1 to i reaches the peak.

3. Descend. Start at i - 1 so the peak is not printed twice, then bare print().

Example 2 — User Input (rows)

Read the row count and draw the same centered palindromic pyramid.

Python
try:
    rows = int(input("Enter the number of rows: "))
except ValueError:
    print("Please enter a positive whole number.")
    raise SystemExit

if rows < 1:
    print("Please enter a positive whole number.")
    raise SystemExit

for i in range(1, rows + 1):
    for s in range(1, rows - i + 1):
        print("  ", end="")

    for k in range(1, i + 1):
        print(str(k) + " ", end="")

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

    print()

How It Works

1. Validate rows. Catch ValueError; require rows >= 1.

2. Same core. Spacing and palindrome loops match Example 1 — only rows comes from the user.

3. Single-digit tip. Cap demos at rows ≤ 9 so every digit stays one character wide.

Example 3 — Compact rows = 3

Same structure with only three rows — easy to trace on paper.

Python
rows = 3

for i in range(1, rows + 1):
    for s in range(1, rows - i + 1):
        print("  ", end="")

    for k in range(1, i + 1):
        print(str(k) + " ", end="")

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

    print()

How It Works

1. Three rows. Row 1: four leading spaces + 1. Row 2: two spaces + 1 2 1.

2. Trace on paper. Confirm descent starts at i - 1 so row 3 is 1 2 3 2 1, not 1 2 3 3 2 1.

Edge Cases & Pitfalls

Check these before calling the solution done.

k = i..1

Double peak

If descent starts at i, row 3 becomes 1 2 3 3 2 1. Always start at i - 1.

one space

Weak centering

Use print(" ", end="") (two spaces) per indent so digits align under each other with str(k) + " ".

bare print()

Broken rows

If bare print() sits inside an inner loop, each digit lands on its own line. Call it only after both digit loops finish.

rows = 1

Single digit

Output is just 1 with no leading spaces — a good sanity check for input validation.

rows > 9

Multi-digit width

Digits 10+ break neat alignment with str(k) + " ". Cap demos at 9 or use a fixed field width.

Bad input

int(input()) throws

Wrap with try/except ValueError so non-numeric input does not crash the script.

Time and Space Complexity

ProgramTimeExtra space
Fixed / compact (Examples 1, 3)O(n²)O(1)
User input (Example 2)O(n²)O(1)

Row i prints O(i) spaces and digits; summing over n rows is O(n²). Only a few loop variables are needed.

Key Takeaways

  • Three parts per row: leading spaces, ascending 1..i, descending i-1..1.
  • Skip the peak: descent starts at i - 1 so the middle digit appears once.
  • print vs print(): spaces and digits use end=""; bare print() advances after the full row.
  • Next step: Program 57 prints the row number only on left and right diagonals.

One line: spaces (rows-i), then 1..i, then i-1..1, then print().

Frequently Asked Questions

Each row reads the same forward and backward: 1, 1 2 1, 1 2 3 2 1, and so on.
Print (rows - i) pairs of spaces before the numbers on row i — more spaces on upper rows, fewer on lower rows.
A centered pyramid where row i shows 1..i ascending then i-1..1 descending, e.g. row 3: ' 1 2 3 2 1 '.
Program 55 fills a 2D list column-wise. Program 56 prints palindromic digits directly with spaces, ascending, and descending loops.
Starting at i-1 avoids printing the peak digit twice — row i already printed i in the ascending loop.
Change rows or read it from user input with int(input()) inside try/except — see Example 2.
O(n²) for n rows because each row prints O(n) spaces and digits.
Yes. Print this pyramid for the top half, then mirror rows from rows-1 down to 1 for the bottom half.
Use try/except ValueError around int(input()) so bad input does not crash the script.
One centered row prints a single 1 with (rows-1)*2 = 0 leading spaces.

Did you know?

Each row prints 1..i..1 with leading spaces for centering. Row i has 2i-1 digits — total work grows as O(n²) for n rows.

Next: Diagonal Mirror Number Pyramid

Continue with a pyramid that prints the row number only on the left and right diagonals.

Program 57 tutorial →

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.

6 people found this page helpful