A palindrome number triangle prints ascending digits 1..i, then mirrors i−1..1 — each row reads the same forwards and backwards.
Remember
Rule: for i = 1..rows
print 1..i, then i−1..1
1
121
12321
1234321
123454321 ← rows = 5
Two inner loops keep the peak digit from repeating — a clear follow-up after the diagonal star in Program 26.
Approach
How to Solve It
For each row i, print 1..i, then print i−1..1, then end the line.
Method
Idea
Best for
Two inner loops
Ascend, then mirror from i - 1
Learning, interviews
input() rows
Same loops; read row count at runtime
Interactive practice
Spaced digits
print(..., end=" ") in both halves
Readable output
Pseudocode
Pseudocode
for i from 1 to rows:
for j from 1 to i:
print j (same line, no spaces)
for k from i - 1 down to 1:
print k (same line, no spaces)
print newline
Cheat sheet
Goal
Pattern
Set rows
rows = 5
Outer loop
for i in range(1, rows + 1):
Ascending half
for j in range(1, i + 1): print(j, end="")
Mirror half
for k in range(i - 1, 0, -1): print(k, end="")
End row
print()
Spaced
print(j, end=" ") / print(k, end=" ")
Printing Numbers vs Starting a New Line
API
Effect
Use for
print(j, end="") / print(k, end="")
Stays on the same line
Each digit
print()
Ends the current line
After both inner loops
Glue digits with end="", then break once with print(). Putting print() inside an inner loop prints one digit per line.
Try it
Live Preview
Change the row count and the palindrome triangle updates instantly.
Whole numbers from 1 to 9. Tap a chip or type a value — the preview redraws as you go.
Live result5 rows · 25 digits
1
121
12321
1234321
123454321
Trace
Worked Walkthrough — rows = 3
Trace each i, the ascending half, the mirror half, and the full row.
i
Ascend 1..i
Mirror i−1..1
Printed row
1
1
none
1
2
12
1
121
3
123
21
12321
Starting the mirror at i − 1 keeps the peak digit from appearing twice in the middle.
Code
Python Programs
Three complete programs: fixed rows = 5, input() rows, and spaced digits. Use View Output for sample results.
Example 1 — Fixed rows = 5
Hard-coded height — ascend 1..i, then mirror from i − 1.
Python
rows = 5
for i in range(1, rows + 1):
for j in range(1, i + 1):
print(j, end="")
for k in range(i - 1, 0, -1):
print(k, end="")
print()
Output
1
121
12321
1234321
123454321
How It Works
1. Outer rows.i runs from 1 to rows — the peak digit of that palindrome.
2. Ascend. Print j from 1 to i with end="".
3. Mirror. Print k from i − 1 down to 1; then print() ends the row.
Example 2 — input() Rows
Read the row count at runtime; both inner loops stay the same.
Python
rows = int(input("Enter rows: "))
for i in range(1, rows + 1):
for j in range(1, i + 1):
print(j, end="")
for k in range(i - 1, 0, -1):
print(k, end="")
print()
Output (when user enters 4)
Enter rows: 4
1
121
12321
1234321
How It Works
1. Read rows. Convert the prompt answer with int().
2. Same two loops. Ascend and mirror logic matches Example 1 for the chosen height.
3. Safer input tip. Bare int(input()) raises ValueError on letters. Prefer:
Safer input
raw = input("Enter rows: ").strip()
try:
rows = int(raw)
except ValueError:
print("Enter a positive whole number.")
raise SystemExit(1)
if rows < 1:
print("Enter a positive whole number.")
raise SystemExit(1)
Example 3 — Spaced Digits
Keep rows = 5 but print a space after each digit in both halves.
Python
rows = 5
for i in range(1, rows + 1):
for j in range(1, i + 1):
print(j, end=" ")
for k in range(i - 1, 0, -1):
print(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. Same structure. Outer loop and both half-loops match Example 1.
2. Only end= changes. Use end=" " in both the ascending and mirror loops.
3. Still a palindrome. Values and length stay the same — only spacing differs.
Edge Cases & Pitfalls
Check these before calling the solution done.
k = i
Doubled peak
Starting the mirror at range(i, 0, -1) repeats the middle digit (123321). Use range(i - 1, 0, -1).
no mirror
Missing second loop
Skipping the descending loop prints only 1, 12, 123 — not palindromes.
print() inside
Vertical digits
If print() is inside either inner loop, each digit lands on its own line. Call it only after both loops.
rows = 1
Single digit
Output is just 1 — the mirror loop never runs.
rows ≤ 0
Empty output
The outer loop never runs. Guard interactive input with rows >= 1.
input()
Catch ValueError
Letters crash bare int(input()) — wrap in try/except and require rows >= 1.
Analysis
Time and Space Complexity
Program
Time
Extra space
Examples 1–3
O(n²)
O(1)
Row i prints 2i − 1 digits; total = 1 + 3 + … + (2n − 1) = n² → O(n²).
Remember
Key Takeaways
Rule: print 1..i, then mirror i − 1..1.
Start at i − 1: avoids doubling the peak digit in the middle.
end= vs print(): digits stay on the line; print() advances after each row.
Complexity:O(n²) for n rows.
One line: climb to i, then walk back down from i − 1 so the row is a palindrome.
Frequently Asked Questions
Starting from i - 1 avoids repeating the middle number. For i = 3, printing 123 then 21 makes 12321.
Use print(j, end=" ") and print(k, end=" ") in both loops instead of print(j, end="") (see Example 3).
The ascending loop prints 1..i and the descending loop prints i - 1..1 — together they read the same forwards and backwards.
print(j, end="") prints digits on the same line. print() ends the row after both inner loops finish.
One loop handles the ascending half (j) and the other handles the mirror half (k) — clearer than a single complex loop.
Replace 5 with rows in the outer loop bound — see Example 2.
O(n²) for n rows because row i prints 2i - 1 digits and the total is 1+3+5+…+(2n-1) = n².
Use try/except ValueError around int(input()) so bad input does not crash the script.
Only one row prints — a single 1 with no mirror half.
🤔
Did you know?
This palindrome triangle prints 1..i and then i-1..1 on each row. The second loop mirrors the first, producing outputs like 12321 and 123454321.