Mirrored Alphabet Pattern in Python

What You'll Learn
This pattern prints an increasing alphabet on the left and a decreasing alphabet on the right, with a shrinking space gap in the middle.
⭐ Pattern Output
When you run the program with rows = 5:
A A
AB BA
ABC CBA
ABCD DCBA
ABCDEEDCBAComplete Python Program
Fixed rows = 5 version (no magic ASCII numbers):
rows = 5
base = ord('A')
top = base + rows - 1 # 'E'
for row in range(rows): # 0..4
left_end = base + row
# Left half: A..(A+row) then spaces to fill width
for code in range(base, top + 1):
print(chr(code) if code <= left_end else " ", end="")
# Right half: E..A then spaces to fill width (same condition)
for code in range(top, base - 1, -1):
print(chr(code) if code <= left_end else " ", end="")
print()🧠 How It Works
How far the alphabet reaches
For each row in 0..rows-1, left_end = base + row is the highest letter printed on that row (A, then B, then C, …).
Left sweep with padding
First loop walks every code from A to top. If code <= left_end, print the letter; else print a space. That leaves a widening gap toward the right half of the row.
Right sweep mirrors the rule
Second loop goes from top down to A with the same condition, producing the descending mirror so spaces shrink until the halves meet on the last row.
Closing mirror
Each row prints 2×rows slots in two passes — O(n²) time for n rows, 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): \"))\nrows = max(1, min(rows, 26))\n\nbase = ord('A')\ntop = base + rows - 1\n\nfor row in range(rows):\n left_end = base + row\n\n for code in range(base, top + 1):\n print(chr(code) if code <= left_end else \" \", end=\"\")\n\n for code in range(top, base - 1, -1):\n print(chr(code) if code <= left_end else \" \", end=\"\")\n\n print()💡 Tips for Enhancement
Try These
- Print lowercase letters by using
ord('a')as the base - Replace spaces with dots to visualize the layout while debugging
- Build each row as a string for easier testing
Avoid
- Hardcoding ASCII numbers like
65or70 - Changing the middle gap without adjusting both halves equally
Key Takeaways
Left side prints A up to a row-specific letter; the rest are spaces.
Right side prints E down to A using the same condition.
The middle gap shrinks by two each row and becomes zero on the last row.
❓ Frequently Asked Questions
ABCDE touches the right EDCBA, producing ABCDEEDCBA.rows = n, the gap starts at 2*(n-1) spaces and decreases by 2 each row until it reaches 0.n rows, because each row prints O(n) characters across both halves.Next: Python Alphabet Pattern 20
Continue to Program 20 for the next alphabet pattern in Python.
10 people found this page helpful
