Reverse Alphabet Pattern (EDCBA to E) in Python

What You'll Learn
This Python program prints a reverse alphabet pattern where each row starts at a fixed top letter (E for rows = 5) and ends one letter earlier each row.
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
EDCB
EDC
ED
EComplete Python Program
Fixed rows = 5 version:
rows = 5
base = ord('A')
top = base + rows - 1 # 'E' when rows = 5
for i in range(rows):
stop = base + i
for code in range(top, stop - 1, -1):
print(chr(code), end="")
print()🧠 How It Works
Compute the top letter
top = ord('A') + rows - 1 sets the fixed starting letter for every row. With rows = 5, it becomes E.
Move the stop letter forward
For row i, we set stop = base + i. That gives stop letters A, B, C, D, E as rows progress.
Print from top down to stop
range(top, stop - 1, -1) prints the descending letters for each row. The final print() starts the next row on a new line.
Shrinking rows from the end
Total prints are 1+2+…+n = n(n + 1)/2, so runtime 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):
stop = base + i
for code in range(top, stop - 1, -1):
print(chr(code), end="")
print()💡 Tips for Enhancement
Try These
- Print lowercase output by using
ord('a')as the base - Add spaces between letters for readability
- Fix the top letter to
Zand clamp rows to keep the stop within the alphabet - Mirror the output to build a diamond pattern
Avoid
- Hardcoding ASCII values like
69or65 - Letting
rowsexceed 26 without defining behavior - Forgetting the newline after each row
- Mixing tabs/spaces in Python examples
Key Takeaways
Compute a fixed top letter using ord('A') + rows - 1.
Move the stop letter forward each row to shrink the line.
Total printed letters are \(n(n+1)/2\), so runtime is O(n²).
Use range(..., ..., -1) for descending sequences.
Clamp user input to 26 to stay within A–Z.
❓ Frequently Asked Questions
range(top, stop - 1, -1) includes the stop letter.end=" " in the inner loop, then print a newline after each row.n rows, because the program prints 1+2+…+n letters in total.Next: Python Alphabet Pattern 9
Continue to Program 9 for the next alphabet pattern in Python.
Python’s range() can count backwards with a negative step, which is handy for reverse alphabet and reverse number patterns.
10 people found this page helpful
