Python Palindrome Number Triangle Pattern (Outer Peak)
Beginner
5 min read
Updated: Sep 2026
3 programs
Live preview
Definition
What Is This Pattern?
A palindrome number triangle prints digits that read the same forward and backward on each row. Build the left half by counting down, then the right half by counting up through 1.
Remember
Rule: print i..2, then 1..i (center is always 1)
1
212
32123
4321234
543212345 ← 5 rows
Follows the right-aligned decreasing triangle in Program 36; next is the decreasing-length continuous triangle in Program 38.
Approach
How to Solve It
One outer loop and two inner loops that mirror around 1 — tight digits with end="", then bare print().
Method
Idea
Best for
Two-loop palindrome
Descend to 2, then ascend from 1
Learning, interviews, exams
User-input rows
Same logic with a variable height
Practice / demos
Pseudocode
Pseudocode
for i from 1 to rows:
for j from i down to 2:
print j
for j from 1 to i:
print j
print newline
Cheat sheet
Goal
Pattern
Grow the row
for i in range(1, rows + 1):
Left half
for j in range(i, 1, -1): print(j, end="")
Right half
for j in range(1, i + 1): print(j, end="")
End the row
print()
Printing Numbers vs Starting a New Line
API
Effect
Use for
print(j, end="")
Stays on the same line
Each digit (no spaces)
print()
Ends the current line
After both inner loops
Print digits without a newline, then end the row once.
Try it
Live Preview
Change the row count and the palindrome triangle updates instantly — including total digit count.
Whole numbers from 1 to 9 (keeps digits single-width). Tap a chip or type a value — the preview redraws as you go.
Live result5 rows · 25 digits
1
212
32123
4321234
543212345
Trace
Worked Walkthrough — rows = 4
Trace the left descent and the right ascent. The center digit is always 1.
i
Left / right
Printed row
1
(none) / 1
1
2
2 / 12
212
3
32 / 123
32123
4
432 / 1234
4321234
The left loop stops at 2 so the ascending loop can print the center 1 once — never twice.
Code
Python Programs
Three complete programs: fixed rows = 5, input() variant, and a compact rows = 3 demo. Use View Output to reveal sample results.
Example 1 — Fixed rows = 5
Two inner loops per row: descend to 2, then ascend from 1 through i.
Python
rows = 5
for i in range(1, rows + 1):
for j in range(i, 1, -1):
print(j, end="")
for j in range(1, i + 1):
print(j, end="")
print()
Output
1
212
32123
4321234
543212345
How It Works
1. Outer loop.i grows from 1 to 5 — each row is a longer palindrome.
2. Left half.j runs from i down to 2 (skipped entirely when i = 1).
3. Right half.j runs from 1 to i, placing the center 1 and the ascending mirror.
Example 2 — User Input Rows
Read rows with input(), validate, then apply the same two-loop palindrome.
Python
try:
rows = int(input("Enter rows: "))
except ValueError:
print("Please enter a positive whole number.")
raise SystemExit(1)
if rows < 1:
print("Please enter a positive whole number.")
raise SystemExit(1)
for i in range(1, rows + 1):
for j in range(i, 1, -1):
print(j, end="")
for j in range(1, i + 1):
print(j, end="")
print()
Output (when user enters 3)
Enter rows: 3
1
212
32123
How It Works
1. Prompt and validate. Catch ValueError; require rows >= 1 before printing.
2. Same core. Descend + ascend match Example 1 — only rows comes from the user.
3. Safer input tip. Cap demos for readable single-digit output:
Safer input tip
if rows < 1 or rows > 9:
print("Enter a whole number from 1 to 9.")
raise SystemExit(1)
Example 3 — Compact rows = 3
Same structure with only three rows — easy to confirm the center 1 is never doubled.
Python
rows = 3
for i in range(1, rows + 1):
for j in range(i, 1, -1):
print(j, end="")
for j in range(1, i + 1):
print(j, end="")
print()
Output
1
212
32123
How It Works
1. Same rules. Left prints i..2; right prints 1..i.
2. Quick check. The longest row is 32123 — five digits for i = 3.
3. Scale up next. Once the small demo is clear, use Examples 1–2 for five rows or user input.
Edge Cases & Pitfalls
Check these before calling the solution done.
double 1
Descend all the way to 1
Using range(i, 0, -1) on the left prints two 1s. Stop at range(i, 1, -1) so the ascending loop owns the center.
newline early
print() between the two halves
That splits the palindrome onto two lines. Call bare print() only after both loops finish.
extra spaces
Print a space after each digit
This pattern is tight digits only. Use print(j, end="") with no trailing space.
rows = 1
Single digit
The left loop does not run; only the ascending loop prints 1.
rows > 9
Multi-digit cells
Values past 9 print as two characters and break the visual palindrome. Cap demos at 9.
input()
Catch ValueError
Bare int(input()) crashes on non-numeric text — wrap it in try/except ValueError.
Analysis
Time and Space Complexity
Program
Time
Extra space
Fixed / input (Examples 1–2)
O(n²)
O(1)
Compact rows = 3 (Example 3)
O(n²)
O(1)
Row i prints 2i - 1 digits. Total = 1 + 3 + … + (2n−1) = n² → O(n²) time. Only a few loop variables are needed.
Remember
Key Takeaways
Rule: print i..2, then 1..i — the center is always 1.
Length: row i has 2i - 1 digits; all rows together print n² digits.
end="" vs print(): digits stay on the line; bare print() advances after both halves.
Complexity:O(n²) time; O(1) extra space.
One line: for each row i, print a descending left half and an ascending right half that meet at 1.
Frequently Asked Questions
A palindrome number triangle: for rows=5 you get 1 / 212 / 32123 / 4321234 / 543212345 — each row reads the same forward and backward.
Each row reads the same forward and backward. For example, 4321234 is symmetric around the center digit 1.
First, a loop prints i down to 2 (left half). Then another loop prints 1 up to i (right half). Together they create a mirrored sequence.
The first loop prints 4 3 2, and the second loop prints 1 2 3 4, which together form 4321234.
Row i prints 2i - 1 digits — total digits across n rows equal n².
Program 36 is right-aligned with decreasing sequences. Program 37 builds a symmetric palindrome on each row with two inner loops.
Use try/except ValueError around int(input()) and require rows >= 1 — see Example 2.
O(n²) for n rows because total digits printed are 1 + 3 + 5 + … + (2n-1) = n².
🤔
Did you know?
Each row is a palindrome: print i down to 2, then 1 up to i. Row i prints 2i - 1 digits — total digits across all rows = n².