Reverse Alphabet Pattern with * Diagonal in Python

Beginner
⏱️ 6 min read
📚 Updated: Aug 2025
🎯 2 Code Examples
star diagonal

What You'll Learn

This program prints E to A on each row, but replaces one position with *. The * shifts each row, creating a diagonal effect.

⭐ Pattern Output

When you run the program with rows = 5:

Output
EDCB*
EDC*A
ED*BA
E*CBA
*DCBA
1

Complete Python Program

Fixed rows = 5 version (no magic ASCII numbers):

Python
rows = 5

base = ord('A')
top = base + rows - 1  # 'E' when rows = 5

for r in range(rows):  # 0..4
    star_code = base + r  # A, B, C, D, E
    for code in range(top, base - 1, -1):  # E..A
        if code == star_code:
            print("*", end="")
        else:
            print(chr(code), end="")
    print()

🧠 How It Works

1

Full reverse row each time

For each row r, the inner loop walks codes from top down to base (e.g. E through A), printing the reverse alphabet segment.

Inner
2

One star position per row

star_code = base + r moves the hole from A toward E as r increases. When code == star_code, print * instead of the letter.

Diagonal
3

New line per row

After the inner loop, print() finishes the row so the star shifts diagonally in the output.

print
=

Sliding star

Each of n rows does O(n) prints — O(n²) total 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 r in range(rows):
    star_code = base + r
    for code in range(top, base - 1, -1):
        print("*" if code == star_code else chr(code), end="")
    print()

💡 Tips for Enhancement

Try These

  • Use lowercase letters by starting from ord('a')
  • Replace * with another symbol like #
  • Reverse the diagonal direction by changing the star position logic

Avoid

  • Hardcoding ASCII numbers like 65 or 69
  • Letting rows exceed 26 without defining wrap-around behavior

Key Takeaways

1

Each row prints letters from top down to A.

2

The star replaces exactly one position per row, creating a diagonal.

3

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

❓ Frequently Asked Questions

We replace the letter when the current descending letter code matches the row’s target code (A, B, C, ...). That makes the * shift each row and form a diagonal.
Yes. Set rows up to 26. Each row prints from top down to A and replaces one position with *.
It’s O(n²) for n rows, because there are n rows and each row prints n characters.

Next: Python Alphabet Pattern 18

Continue to Program 18 for the next alphabet pattern in Python.

Program 18 →

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