Reverse Alphabet Pattern (EDCBA to A) in Python

What You'll Learn
This Python program prints a reverse alphabet pattern where each row starts from a decreasing letter and ends at A.
For n rows, the total letters printed are 1 + 2 + … + n = n(n + 1)/2.
⭐ Pattern Output
When you run the program with rows = 5:
EDCBA
DCBA
CBA
BA
AComplete Python Program
Fixed rows = 5 version:
rows = 5
base = ord('A')
top = base + rows - 1 # 'E' when rows = 5
for i in range(rows): # 0..4
start = top - i
for code in range(start, base - 1, -1):
print(chr(code), end="")
print()🧠 How It Works
Letter range with ord()
base = ord('A') and top = base + rows - 1 map the first row start to the top letter (e.g. E when rows = 5).
Outer loop: row start moves down
for i in range(rows) runs once per row. start = top - i so the first character printed shifts E → D → C … each line.
Inner loop: print down to A
for code in range(start, base - 1, -1) walks codes from the row start down to A; chr(code) turns each into a letter.
Shrinking reverse rows
Row lengths are n, n−1, …, 1. Total letters are n(n+1)/2, so time is O(n²) and extra space is O(1).
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')
top = base + rows - 1
for i in range(rows):
start = top - i
for code in range(start, base - 1, -1):
print(chr(code), end="")
print()💡 Tips for Enhancement
Try These
- Print lowercase output using
ord('a') - Add spaces between letters for readability
- Start from a fixed top letter like
Z - Mirror the output to build a diamond-like shape
Avoid
- Hardcoding ASCII values like
69 - Letting
rowsexceed 26 without handling wrap-around - Forgetting the newline after each row
Key Takeaways
Row i starts at top - i and counts down to A.
Use range(..., ..., -1) for descending sequences.
Total printed letters are \(n(n+1)/2\), so runtime is O(n²).
❓ Frequently Asked Questions
rows = 5, the top letter is computed as 'A' + (rows - 1), which is E. The first row prints from that top letter down to A.top = ord('A') + rows - 1. For row i (0-based), print from top - i down to ord('A').n rows, because the program prints 1+2+…+n letters in total.Next: Python Alphabet Pattern 8
Continue to Program 8 for another reverse alphabet pattern.
10 people found this page helpful
