Alphabet-Star Mirror Pattern (ABCDEEDCBA) in Python

What You'll Learn
This Python program prints an alphabet mirror on both sides and fills the middle with stars. As rows progress, letters shrink and the star block grows.
⭐ Pattern Output
When you run the program with rows = 5:
ABCDEEDCBA
ABCD**DCBA
ABC****CBA
AB******BA
A********AComplete Python Program
Nested loops version:
rows = 5
base = ord('A')
for i in range(rows, 0, -1):
# Left: A..(A+i-1)
for code in range(base, base + i):
print(chr(code), end="")
# Middle: 2*(rows-i) stars
stars = 2 * (rows - i)
for _ in range(stars):
print("*", end="")
# Right: (A+i-1)..A
for code in range(base + i - 1, base - 1, -1):
print(chr(code), end="")
print()🧠 How It Works
Shrink the letter span each row
for i in range(rows, 0, -1) uses i as how many letters appear on each side. Row 5 prints A–E forward and E–A backward; later rows use fewer letters.
Left then stars then right
First loop: base to base + i - 1. Middle: 2 * (rows - i) stars. Last loop: codes from base + i - 1 down to A.
Mirror symmetry
The right loop is the reverse of the left prefix, so the row reads the same forward and backward except for the growing star block in the center.
Stars fill the gap
Each row still does O(rows) prints; over n rows that is O(n²) time and O(1) extra space.
Variation — User Input Version
Read rows from user input (clamped to 26):
rows = int(input("Enter the number of rows (max 26): "))
rows = max(1, min(rows, 26))
base = ord('A')
for i in range(rows, 0, -1):
for code in range(base, base + i):
print(chr(code), end="")
stars = 2 * (rows - i)
print("*" * stars, end="")
for code in range(base + i - 1, base - 1, -1):
print(chr(code), end="")
print()💡 Tips for Enhancement
Try These
- Use lowercase by switching
ord('A')toord('a') - Change the filler character from
*to-or# - Add spaces between letters for readability
- Build each row as a string (useful for unit tests)
Avoid
- Hardcoding ASCII values like
65 - Letting
rowsexceed 26 without defining behavior - Forgetting the newline after each row
Key Takeaways
Left side prints ascending letters; right side prints descending letters.
Stars in the middle are 2*(rows-i).
Use ord()/chr() instead of ASCII numbers.
❓ Frequently Asked Questions
A..E and the right side prints E..A, so the top letter appears twice in the middle.n and current i (counting down), the middle has 2*(n-i) stars.n rows, because total printed characters grow proportionally to n².Next: Python Alphabet Pattern 16
Continue to Program 16 for the next alphabet pattern in Python.
10 people found this page helpful
