Reverse Alphabet Pattern with * Diagonal in Python

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:
EDCB*
EDC*A
ED*BA
E*CBA
*DCBAComplete Python Program
Fixed rows = 5 version (no magic ASCII numbers):
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
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.
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.
New line per row
After the inner loop, print() finishes the row so the star shifts diagonally in the output.
Sliding star
Each of n rows does O(n) prints — O(n²) total 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 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
65or69 - Letting
rowsexceed 26 without defining wrap-around behavior
Key Takeaways
Each row prints letters from top down to A.
The star replaces exactly one position per row, creating a diagonal.
Time complexity is O(n²) for n rows.
❓ Frequently Asked Questions
A, B, C, ...). That makes the * shift each row and form a diagonal.rows up to 26. Each row prints from top down to A and replaces one position with *.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.
10 people found this page helpful
