Palindromic Number Triangle in Python

Beginner
⏱️ 6 min read
📚 Updated: Aug 2025
🎯 2 Code Examples
Nested Loops

What You’ll Learn

How to print a palindromic number triangle in Python where each row counts down to 1 and then counts back up.

For example, row 5 prints 543212345. This is a great pattern for practicing two inner loops and symmetry.

⭐ Pattern Output

For rows = 5, the pattern looks like this:

Output
1
212
32123
4321234
543212345
1

Complete Python Program

Print the left half by counting down from i to 1, then print the right half by counting up from 2 to i.

Python
rows = 5

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

🧠 How It Works

1

Set the number of rows

rows = 5 decides how many lines will be printed.

Setup
2

Outer loop (row index)

for i in range(1, rows + 1) increases the width of the palindrome by one digit each row.

Row control
3

Left half (descending)

for j in range(i, 0, -1) prints i down to 1.

Left side
4

Right half (ascending)

for k in range(2, i + 1) prints 2 up to i, avoiding a double 1 in the center.

Right side
=

Palindromic triangle

Total printed digits grow like 1+3+5+…+(2n-1) = , so the time complexity is O(n²).

2

Variation — User Input Version

Let the user choose the number of rows at runtime:

Python
rows = int(input("Enter the number of rows: "))

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

💡 Tips for Enhancement

Try These

  • Validate input (reject rows < 1) before printing
  • Add spaces between digits using print(j, end=" ")
  • Center the triangle by printing leading spaces before each row
  • Replace digits with characters for an alphabet palindrome pattern
  • Build a diamond by printing the triangle up and then down

Avoid

  • Printing the center digit twice (start the second loop from 2)
  • Forgetting print() after each row
  • Mixing up descending and ascending loops (keep them separate)
  • Assuming user input is always valid (wrap int() if needed)

Key Takeaways

1

Use one loop to print descending digits from i to 1.

2

Use a second loop to print ascending digits from 2 to i.

3

Starting the second loop at 2 prevents duplicating the center 1.

4

Output size grows as , so runtime is O(n²).

❓ Frequently Asked Questions

Because it prints 3 2 1 (descending) and then 2 3 (ascending) to complete the palindrome.
Use print(j, end=" ") and print(k, end=" ") inside the loops.
Yes. Print rows - i leading spaces before the digits on each row.
O(n²), because row i prints about 2i-1 digits and the total is \(1+3+\dots+(2n-1)=n^2\).

Explore More Python Number Patterns!

From pyramids to Floyd’s triangle and beyond—practice nested loops with progressively richer patterns.

All Number Patterns →
Did you know?

The sequence of printed digits per row is odd: 1, 3, 5, 7, … That’s why the total digits after n rows is exactly .

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