Reverse Alphabet Pyramid (E to A) in Python

What You'll Learn
This program prints a reverse alphabet pyramid using nested loops. With rows = 5 it starts at E and expands: E, E D, E D C, E D C B, E D C B A.
⭐ Pattern Output
When you run the program with rows = 5:
E
E D
E D C
E D C B
E D C B AComplete Python Program
Fixed rows = 5 version (same idea as the classic ASCII approach, but using ord()/chr()):
rows = 5
base = ord('A')
top = base + rows - 1 # 'E' when rows = 5
for i in range(top, base - 1, -1): # E..A (controls row)
code = top + 1 # reset per row (like k = 70)
for j in range(base, top + 1): # A..E (controls column)
if j >= i:
code -= 1
print(chr(code), end=" ")
else:
print(" ", end=" ")
print()🧠 How It Works
Top and base letters
base = ord('A') and top = base + rows - 1 set the letter range for the pyramid (A through E for 5 rows).
Outer loop controls how many letters appear
for i in range(top, base - 1, -1) moves the threshold from E down to A. As i decreases, more columns satisfy j >= i, so each row prints one more letter.
Reset and print descending letters
Each row resets code = top + 1. For every column where j >= i, the program decrements code and prints chr(code), producing E D C ... on that row.
Right-aligned reverse pyramid
Row lengths are 1..n, so total letters are n(n+1)/2. The nested loops scan n columns per row, so time is O(n²) with O(1) extra space.
Variation — User Input Version
Read rows from input. Clamped to 1–6 so n(n+1)/2 letters stay within A–Z (6 rows → 21 letters):
rows = int(input("Enter number of rows (max 6): "))
rows = max(1, min(rows, 6))
base = ord('A')
top = base + rows - 1
for i in range(top, base - 1, -1):
code = top + 1
for j in range(base, top + 1):
if j >= i:
code -= 1
print(chr(code), end=" ")
else:
print(" ", end=" ")
print()💡 Tips for Enhancement
Try These
- Use lowercase by setting
base = ord('a') - Replace the inner printing with building a list, then
joinit for cleaner spacing - If you need more than 6 rows, wrap letters with modulo arithmetic
Avoid
- Hardcoding ASCII like
70or69instead of usingord() - Forgetting to reset the per-row counter — otherwise rows won’t start at the top letter
Key Takeaways
Use top = ord('A') + rows - 1 to compute the starting letter.
Reset code per row so each row begins at the same top letter.
Total letters are n(n+1)/2, so keep rows small unless you wrap.
❓ Frequently Asked Questions
code to top + 1 at the start of every row and then prints descending letters where j >= i, making the first printed letter always E.end=\" \" to end=\"\" for letters, and adjust padding so the alignment still looks centered.n rows because each row scans all columns.Next: Python Alphabet Pattern 24
Continue to Program 24 for the next alphabet pattern in Python.
10 people found this page helpful
