Alphabet Palindrome Pattern in Python

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:
A
ABA
ABCBA
ABCDCBA
ABCDEDCBAComplete Python Program
Fixed rows = 5 version (no magic ASCII numbers):
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
Growing peak each row
for row in range(1, rows + 1) sets the peak letter at base + row - 1 (A, B, C, …).
Left half: A to peak
First loop prints codes base through base + row - 1 inclusive — the ascending part including the center letter.
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.
Palindrome rows
Row r prints 2r − 1 letters; summing gives n² letters for n rows — O(n²) time, O(1) extra space.
Variation — User Input Version
Read rows from input (clamped to 26):
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
Left half prints A up to the peak letter for that row.
Right half prints back down from peak-1 to A.
Total characters per row are 2*row - 1.
❓ Frequently Asked Questions
peak - 1) to avoid duplicating the center character, so ABC + BA becomes ABCBA.Z, or implement wrap-around logic.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.
10 people found this page helpful
