Python Palindrome Number Triangle Pattern

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

What Is This Pattern?

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.

How to Solve It

For each row i, print 1..i, then print i−1..1, then end the line.

MethodIdeaBest for
Two inner loopsAscend, then mirror from i - 1Learning, interviews
input() rowsSame loops; read row count at runtimeInteractive practice
Spaced digitsprint(..., end=" ") in both halvesReadable 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

GoalPattern
Set rowsrows = 5
Outer loopfor i in range(1, rows + 1):
Ascending halffor j in range(1, i + 1): print(j, end="")
Mirror halffor k in range(i - 1, 0, -1): print(k, end="")
End rowprint()
Spacedprint(j, end=" ") / print(k, end=" ")

Printing Numbers vs Starting a New Line

APIEffectUse for
print(j, end="") / print(k, end="")Stays on the same lineEach digit
print()Ends the current lineAfter both inner loops

Glue digits with end="", then break once with print(). Putting print() inside an inner loop prints one digit per line.

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 result 5 rows · 25 digits
1
121
12321
1234321
123454321

Worked Walkthrough — rows = 3

Trace each i, the ascending half, the mirror half, and the full row.

iAscend 1..iMirror i−1..1Printed row
11none1
2121121
31232112321

Starting the mirror at i − 1 keeps the peak digit from appearing twice in the middle.

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()

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()

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()

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.

Time and Space Complexity

ProgramTimeExtra space
Examples 1–3O(n²)O(1)

Row i prints 2i − 1 digits; total = 1 + 3 + … + (2n − 1) = n² → O(n²).

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.

Next: 0-Centered Descending Mirror

Continue with the next pattern in the Python number-pattern series.

Program 28 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.

12 people found this page helpful