Reverse Alphabet Triangle (A to EDCBA) in Python

What You'll Learn
This Python program prints a triangle pattern where each row starts from the current letter and counts down to A.
For n rows, the total letters printed are 1 + 2 + … + n = n(n + 1)/2.
⭐ Pattern Output
When you run the program with rows = 5:
A
BA
CBA
DCBA
EDCBAComplete Python Program
Fixed rows = 5 version:
rows = 5
base = ord('A')
for i in range(rows):
for code in range(base + i, base - 1, -1):
print(chr(code), end="")
print()🧠 How It Works
Choose the base letter
base = ord('A') stores the numeric code for A. Using ord() avoids magic numbers like 65.
Outer loop: row index
for i in range(rows) runs from 0 to rows - 1. Row i prints i + 1 letters.
Inner loop: count down to A
range(base + i, base - 1, -1) counts down from the row’s starting letter to A (inclusive). Each chr(code) converts a number back to a letter.
Reverse rows ending at A
Total prints are 1+2+…+n = n(n + 1)/2, so runtime is O(n²) and extra space is O(1).
Variation — User Input Version
Read rows from user input (clamped to 26):
rows = int(input("Enter the number of rows (max 26): "))
rows = max(1, min(rows, 26))
base = ord('A')
for i in range(rows):
for code in range(base + i, base - 1, -1):
print(chr(code), end="")
print()💡 Tips for Enhancement
Try These
- Print lowercase by changing
ord('A')toord('a') - Add spaces between letters (e.g.,
end=" ") - Start from a different base letter (like
'C') - Try mirroring the triangle to create a diamond
Avoid
- Hardcoding ASCII values like
65 - Allowing
rows > 26without defining behavior - Forgetting to print a newline after each row
- Mixing tabs/spaces in Python code examples
Key Takeaways
Row i starts at A + i and counts down to A.
Use range(start, stop, -1) for descending sequences.
Total printed letters are \(n(n+1)/2\), so runtime is O(n²).
Use ord()/chr() instead of ASCII numbers.
Clamp user input to 26 rows to stay within the alphabet.
❓ Frequently Asked Questions
print(chr(code), end=" ") inside the inner loop, then print a newline after the row.base = ord('A') to your preferred starting letter, and adjust the row limit so it doesn’t go past Z.n rows, because the program prints 1+2+…+n letters in total.Next: Python Alphabet Pattern 5
Continue to Program 5 for the next alphabet pattern in Python.
Reverse ranges in Python (negative step) are also used for countdown timers and reverse array indexing — not just patterns.
10 people found this page helpful
