Mixed Alphabet Pattern in Python

What You'll Learn
This program prints rows like ABCDE, BABCD, CBABC, DCBAB, EDCBA by combining a descending prefix down to A and an ascending suffix.
⭐ Pattern Output
When you run the program with rows = 5:
ABCDE
BABCD
CBABC
DCBAB
EDCBAComplete Python Program
Fixed rows = 5 version (no magic ASCII values like 64):
rows = 5
rows = max(1, min(rows, 26))
base = ord('A')
top = base + rows - 1
for r in range(rows): # 0..4
start = base + r # A, B, C, D, E
end = top - r # E, D, C, B, A
# Descending prefix: start..A
for code in range(start, base - 1, -1):
print(chr(code), end="")
# Ascending suffix: B..end (skip A)
for code in range(base + 1, end + 1):
print(chr(code), end="")
print()🧠 How It Works
Start and end letters per row
For row index r, start = A + r moves forward, while end = top - r moves backward (E, D, C, …).
Descending prefix down to A
for code in range(start, A-1, -1) prints start down to A. Example: for start = C it prints CBA.
Ascending suffix from B
Then we print from B up to end. Starting from B avoids duplicating A in the middle.
Shrinking suffix
Each row prints at most O(rows) letters, and there are rows rows, so time is O(n²) and extra space is O(1).
Variation — User Input Version
Read rows from input (clamped to 26):
rows = int(input("Enter number of rows (max 26): "))
rows = max(1, min(rows, 26))
base = ord('A')
top = base + rows - 1
for r in range(rows):
start = base + r
end = top - r
for code in range(start, base - 1, -1):
print(chr(code), end="")
for code in range(base + 1, end + 1):
print(chr(code), end="")
print()💡 Tips for Enhancement
Try These
- Add spaces between letters for readability
- Use lowercase by setting
base = ord('a') - Center the output with leading spaces per row
Avoid
- Printing
Atwice in the middle (start the suffix at B) - Hardcoding ASCII values like 64 or 70
Key Takeaways
Prefix prints from the row letter down to A.
Suffix prints from B up to a shrinking end letter.
Overall runtime is O(n²).
❓ Frequently Asked Questions
end=\" \" or build the row in a list and use \" \".join(letters).end = top - r decreases as r increases, shrinking the ascending part.n rows.Next: Python Alphabet Pattern 31
Continue to Program 31 for the next alphabet pattern in Python.
10 people found this page helpful
