Decreasing Alphabet Pattern (ABCDE to A) in Python

What You'll Learn
This Python program prints a decreasing alphabet pattern. The first row prints rows letters from A, and each next row prints one less.
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:
ABCDE
ABCD
ABC
AB
AComplete Python Program
Fixed rows = 5 version:
rows = 5
base = ord('A')
for i in range(rows, 0, -1):
for code in range(base, base + i):
print(chr(code), end="")
print()🧠 How It Works
Set a base letter
base = ord('A') stores the numeric code for A. We’ll print letters by converting back using chr().
Outer loop: decreasing row length
for i in range(rows, 0, -1) produces rows, rows-1, ..., 1. So each next row is shorter by one letter.
Inner loop: print A to A+i-1
range(base, base + i) iterates through the first i letter codes starting from A. Using end="" keeps the row on one line.
Decreasing rows
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, 0, -1):
for code in range(base, base + i):
print(chr(code), end="")
print()💡 Tips for Enhancement
Try These
- Print lowercase output by using
ord('a') - Add spaces between letters for readability
- Start from a different letter and limit the max rows accordingly
- Try printing the decreasing pattern in reverse (A, AB, ABC, ...)
Avoid
- Hardcoding ASCII values like
65 - Letting
rowsexceed 26 without defining behavior - Forgetting a newline after each row
- Mixing tabs/spaces in Python code examples
Key Takeaways
Use a decreasing outer loop to shorten each row.
The inner loop prints from A up to the row end letter.
Total printed letters are \(n(n+1)/2\), so runtime is O(n²).
Use ord()/chr() for clean character arithmetic.
Clamp input to 26 to stay within A–Z.
❓ Frequently Asked Questions
print(chr(code), end=" ") inside the inner loop, then print a newline after the row.A to Z without wrapping.n rows, because the program prints 1+2+…+n letters in total.Next: Python Alphabet Pattern 6
Continue to Program 6 for the next alphabet pattern in Python.
Decreasing patterns like this are a great way to practice loop bounds and end-exclusive ranges in Python.
10 people found this page helpful
