Alphabet Mirror Pattern in Python

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:
A
BAB
CBABC
DCBABCD
EDCBABCDEComplete Python Program
Fixed rows = 5 version (same structure as the reference, but without magic ASCII values):
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
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.
Left half: descending to A
for j in range(i, base - 1, -1) prints i, i-1, ... A. Example for C: prints CBA.
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.
Mirror rows
Row r prints 2r - 1 characters; total is n² for n rows. Runtime is O(n²) and extra space is O(1).
Variation — User Input Version
Read rows from input (clamped to 26 so we stay within A–Z):
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
Atwice in the middle (start the second loop atB) - Hardcoding ASCII numbers instead of using
ord()/chr()
Key Takeaways
The first loop prints i..A (descending).
The second loop prints B..i to mirror the row without duplicating A.
Total output characters scale as n² for n rows.
❓ Frequently Asked Questions
A twice in the center. Descending prints ...A, and the ascending half should begin at B.end=\" \" and adjust your joins/padding to keep lines aligned.Next: Python Alphabet Pattern 25
Continue to Program 25 for the next alphabet pattern in Python.
10 people found this page helpful
