Alphabet Mirror Pattern in Python

Beginner
⏱️ 6 min read
📚 Updated: Aug 2025
🎯 2 Code Examples
down then up

What You'll Learn

This program prints a mirror (palindrome) alphabet pattern. Each row starts at the row’s letter, goes down to A, then returns back up, forming rows like BAB and CBABC.

⭐ Pattern Output

When you run the program with rows = 5:

Output
A
BAB
CBABC
DCBABCD
EDCBABCDE
1

Complete Python Program

Fixed rows = 5 version (same structure as the reference, but without magic ASCII values):

Python
rows = 5

base = ord('A')
top = base + rows - 1  # 'E' when rows = 5

for i in range(base, top + 1):        # A..E
    for j in range(i, base - 1, -1):  # i..A (descending)
        print(chr(j), end="")
    for k in range(base + 1, i + 1):  # B..i (ascending, skip A)
        print(chr(k), end="")
    print()

🧠 How It Works

1

Choose the row letter

for i in range(base, top + 1) picks the row’s highest letter: A, then B, then C, and so on.

Outer
2

Left half: descending to A

for j in range(i, base - 1, -1) prints i, i-1, ... A. Example for C: prints CBA.

Down
3

Right half: ascend back (skip A)

for k in range(base + 1, i + 1) prints B..i so A is not duplicated. For C: prints BC, making CBABC.

Up
=

Mirror rows

Row r prints 2r - 1 characters; total is for n rows. Runtime is O(n²) and extra space is O(1).

2

Variation — User Input Version

Read rows from input (clamped to 26 so we stay within A–Z):

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

base = ord('A')
top = base + rows - 1

for i in range(base, top + 1):
    for j in range(i, base - 1, -1):
        print(chr(j), end="")
    for k in range(base + 1, i + 1):
        print(chr(k), end="")
    print()

💡 Tips for Enhancement

Try These

  • Print spaces between letters (use end=\" \") for readability
  • Use lowercase by setting base = ord('a')
  • Center the output by adding leading spaces for each row

Avoid

  • Printing A twice in the middle (start the second loop at B)
  • Hardcoding ASCII numbers instead of using ord()/chr()

Key Takeaways

1

The first loop prints i..A (descending).

2

The second loop prints B..i to mirror the row without duplicating A.

3

Total output characters scale as for n rows.

❓ Frequently Asked Questions

To avoid printing A twice in the center. Descending prints ...A, and the ascending half should begin at B.
Yes. Print with end=\" \" and adjust your joins/padding to keep lines aligned.
It’s O(n²) because row r prints \(2r-1\) characters and there are \(n\) rows.

Next: Python Alphabet Pattern 25

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

Program 25 →

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