Reverse Alphabet Pattern (E to EDCBA) in Python

What You'll Learn
This Python program prints a reverse alphabet pattern where each row starts from a fixed letter (E for rows = 5) and grows by one descending letter.
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:
E
ED
EDC
EDCB
EDCBAComplete Python Program
Fixed rows = 5 version (computed top letter):
rows = 5
top = ord('A') + rows - 1 # 'E' when rows = 5
for i in range(1, rows + 1):
for code in range(top, top - i, -1):
print(chr(code), end="")
print()🧠 How It Works
Pick the top letter
top = ord('A') + rows - 1 selects the starting letter for every row. For rows = 5, the top becomes ord('E').
Outer loop: one row per i
for i in range(1, rows + 1) runs once per row. Row i prints exactly i letters.
Inner loop: descending codes
range(top, top - i, -1) generates top, top-1, ..., top-i+1. Each chr(code) prints a letter, and print(..., end="") keeps the row on one line.
Descending alphabet rows
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))
top = ord('A') + rows - 1
for i in range(1, rows + 1):
for code in range(top, top - i, -1):
print(chr(code), end="")
print()💡 Tips for Enhancement
Try These
- Start from a fixed letter (e.g., always
'Z') instead of tying it to rows - Print lowercase by using
ord('a')as the base - Add spaces between letters for readability
- Try reversing each row to get
DE,CDE, ...
Avoid
- Hardcoding ASCII values like
69whenord()is clearer - Allowing
rows > 26without defining wrap-around behavior - Forgetting to print a newline after each row
- Building huge strings in a loop when printing directly is enough
Key Takeaways
Compute the top letter from rows using ord('A') + rows - 1.
Row i prints i letters in descending order.
Total printed letters are \(n(n+1)/2\), so runtime is O(n²).
range(..., ..., -1) is the key for reverse sequences.
Clamp user input to 26 rows to stay within A to Z.
❓ Frequently Asked Questions
top = ord('Z') and keep the same inner loop. You may also clamp rows so you donโt go before A.top, steps by -1, and stops before top - i. That yields exactly i values: top down to top - i + 1.n rows, because the program prints 1+2+…+n letters in total.Next: Python Alphabet Pattern 3
Continue to Program 3 for the next alphabet pattern in Python.
In Python, you can iterate backwards easily with range(start, stop, -1) — perfect for reverse alphabet and reverse number patterns.
10 people found this page helpful
