Reverse Alphabet Stair Pattern in Python

Beginner
⏱️ 6 min read
📚 Updated: Aug 2025
🎯 2 Code Examples
right-aligned

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:

Output
    A
   BA
  CBA
 DCBA
EDCBA
1

Complete Python Program

Fixed rows = 5 version (matches the reference logic, without ASCII constants):

Python
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

1

Row’s highest letter

For row in 0..rows-1, current = base + row is the rightmost letter on that line (A, B, …, E).

Outer
2

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.

Inner
3

Next row

print() after the inner loop starts a new line; as current grows, more letters print from the right.

print
=

Right-aligned stair

n rows each scan n columns — O(n²) time, O(1) extra space.

2

Variation — User Input Version

Read rows from input (clamped to 26):

Python
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

1

Row r prints letters from A+r down to A.

2

Leading spaces keep the pattern right-aligned.

3

Time complexity is O(n²) for n rows.

❓ Frequently Asked Questions

Those spaces right-align the pattern in a fixed width. Each next row prints one more letter, so fewer leading spaces are needed.
Yes. Print only the letters for the row (from A+r down to A) and skip the space-filling part.
It’s O(n²) for 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.

Program 21 →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

10 people found this page helpful