Reverse Alphabet Stair Pattern in Python

What You'll Learn
This program prints a right-aligned reverse alphabet pattern. Each row prints letters in descending order ending with A.
⭐ Pattern Output
When you run the program with rows = 5:
A
BA
CBA
DCBA
EDCBAComplete Python Program
Fixed rows = 5 version (matches the reference logic, without ASCII constants):
rows = 5
base = ord('A')
top = base + rows - 1 # 'E' when rows = 5
for row in range(rows): # 0..4
current = base + row # A..E
for code in range(top, base - 1, -1): # E..A
print(chr(code) if code <= current else " ", end="")
print()🧠 How It Works
Row’s highest letter
For row in 0..rows-1, current = base + row is the rightmost letter on that line (A, B, …, E).
Scan full width, pad with spaces
Inner loop runs code from top down to A. Print the letter only when code <= current; otherwise print a space. Early codes are above current, which right-aligns the segment.
Next row
print() after the inner loop starts a new line; as current grows, more letters print from the right.
Right-aligned stair
n rows each scan n columns — O(n²) time, 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): "))
rows = max(1, min(rows, 26))
base = ord('A')
top = base + rows - 1
for row in range(rows):
current = base + row
for code in range(top, base - 1, -1):
print(chr(code) if code <= current else " ", end="")
print()💡 Tips for Enhancement
Try These
- Print lowercase letters by using
ord('a')as the base - Remove alignment spaces for a compact left-aligned version
- Replace spaces with dots while debugging the shape
Avoid
- Hardcoding ASCII values like
69 - Letting rows exceed 26 without defining wrap-around behavior
Key Takeaways
Row r prints letters from A+r down to A.
Leading spaces keep the pattern right-aligned.
Time complexity is O(n²) for n rows.
❓ Frequently Asked Questions
A+r down to A) and skip the space-filling part.n rows, because each row scans a width of n positions.Next: Python Alphabet Pattern 21
Continue to Program 21 for the next alphabet pattern in Python.
10 people found this page helpful
