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.
In Python print (rows - i) pairs of spaces with print(" ", end=""), then print(str(k) + " ", end="") ascending and descending, then a bare print().
Approach
How to Solve It
Per row: center with leading spaces, climb to the peak, then mirror down without repeating the peak.
Method
Idea
Best for
Three inner loops
Spaces, ascending 1..i, descending i-1..1
Learning, interviews, exams
Rows input
Same logic with a user-chosen height
Practice / 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
Goal
Pattern
Outer loop
for i in range(1, rows + 1):
Leading spaces
for s in range(1, rows - i + 1): print(" ", end="")
Ascending half
for k in range(1, i + 1): print(str(k) + " ", end="")
Descending half
for k in range(i - 1, 0, -1): print(str(k) + " ", end="")
Digits on row i
2 * i - 1
Printing Numbers vs Starting a New Line
API
Effect
Use for
print(..., end="")
Stays on the same line
Spaces and each digit plus trailing space
print()
Ends the current line
After spaces + ascending + descending
Spaces and digits use end=""; a bare print() ends the row once.
Try it
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 resultrows = 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
Trace
Worked Walkthrough — Row i = 3 when rows = 5
Trace one middle row so centering and the skip-peak descent are clear.
Step
Loop
Prints
1
s = 1..2 (rows - i)
(4 spaces)
2
k = 1..3
1 2 3
3
k = 2..1
2 1
Full row: 1 2 3 2 1. Peak 3 appears once because descent starts at i - 1.
Code
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()
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
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()
Output (when user enters 4)
Enter the number of rows: 4
1
1 2 1
1 2 3 2 1
1 2 3 4 3 2 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()
Output
1
1 2 1
1 2 3 2 1
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.
Analysis
Time and Space Complexity
Program
Time
Extra 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.
Remember
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.