Alphabet Palindrome Pattern in Python

Beginner
⏱️ 6 min read
📚 Updated: Aug 2025
🎯 2 Code Examples
1, 3, 5, ...

What You'll Learn

This pattern prints a row that increases from A to a peak letter, then decreases back to A — forming a palindrome like ABCBA.

⭐ Pattern Output

When you run the program with rows = 5:

Output
A
ABA
ABCBA
ABCDCBA
ABCDEDCBA
1

Complete Python Program

Fixed rows = 5 version (no magic ASCII numbers):

Python
rows = 5

base = ord('A')

for row in range(1, rows + 1):
    # Left: A..peak
    for code in range(base, base + row):
        print(chr(code), end="")

    # Right: (peak-1)..A
    for code in range(base + row - 2, base - 1, -1):
        print(chr(code), end="")

    print()

🧠 How It Works

1

Growing peak each row

for row in range(1, rows + 1) sets the peak letter at base + row - 1 (A, B, C, …).

Outer
2

Left half: A to peak

First loop prints codes base through base + row - 1 inclusive — the ascending part including the center letter.

Up
3

Right half: mirror without duplicating peak

Second loop runs from base + row - 2 down to A, printing the descending side so the row reads the same forward and backward.

Down
=

Palindrome rows

Row r prints 2r − 1 letters; summing gives letters for n rows — O(n²) time, O(1) extra space.

2

Variation — User Input Version

Read rows from input (clamped to 26):

Python
rows = int(input("Enter the number of rows (max 26): "))
rows = max(1, min(rows, 26))

base = ord('A')

for row in range(1, rows + 1):
    for code in range(base, base + row):
        print(chr(code), end="")
    for code in range(base + row - 2, base - 1, -1):
        print(chr(code), end="")
    print()

💡 Tips for Enhancement

Try These

  • Print lowercase output using ord('a')
  • Add spaces between letters: print with end=\" \"
  • Center the palindrome to form a pyramid

Avoid

  • Hardcoding ASCII values like 65
  • Duplicating the peak letter on both halves (unless you want ABCCBA)

Key Takeaways

1

Left half prints A up to the peak letter for that row.

2

Right half prints back down from peak-1 to A.

3

Total characters per row are 2*row - 1.

❓ Frequently Asked Questions

The right half starts from the previous letter (peak - 1) to avoid duplicating the center character, so ABC + BA becomes ABCBA.
Clamp rows to 26 so the peak letter doesn't go beyond Z, or implement wrap-around logic.
It’s O(n²) for n rows, because total printed characters are 1+3+5+…+(2n-1) = n².

Next: Python Alphabet Pattern 19

Continue to Program 19 for the next alphabet pattern in Python.

Program 19 →

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.

10 people found this page helpful