Alphabet Pattern (ABCDE to E) in Python

What You'll Learn
This Python program prints an alphabet pattern where every row ends at a fixed letter (E for rows = 5) and the start letter moves forward each row.
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
BCDE
CDE
DE
EComplete Python Program
Fixed rows = 5 version:
rows = 5
base = ord('A')
end = base + rows - 1 # 'E' when rows = 5
for i in range(rows):
start = base + i
for code in range(start, end + 1):
print(chr(code), end="")
print()🧠 How It Works
Compute the end letter
end = ord('A') + rows - 1 sets a fixed row ending letter. With rows = 5, that end becomes E.
Move start forward each row
For row i, we set start = base + i. This produces starts A, B, C, D, E.
Print from start to end
range(start, end + 1) prints letters from the row start through the fixed end letter. The final print() moves to the next line.
Shorter rows that still end at E
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')
end = base + rows - 1
for i in range(rows):
start = base + i
for code in range(start, end + 1):
print(chr(code), end="")
print()💡 Tips for Enhancement
Try These
- Print lowercase letters by using
ord('a')as the base - Add spaces between letters for readability
- Fix the end letter to
Zand clamp rows to avoid going beyond the alphabet - Reverse each row to get
EDCBA,EDCB, ...
Avoid
- Hardcoding ASCII values like
65or70 - Letting
rowsexceed 26 without defining behavior - Forgetting the newline after each row
- Mixing tabs/spaces in Python examples
Key Takeaways
Compute a fixed end letter based on rows.
Move the start letter forward one step per row.
Total printed letters are \(n(n+1)/2\), so runtime is O(n²).
Use ord()/chr() for clean character arithmetic.
Clamp user input to 26 to stay within A–Z.
❓ Frequently Asked Questions
range(start, end + 1) includes the end letter.end=" " in the inner loop, then print a newline after each row.n rows, because the program prints 1+2+…+n letters in total.Next: Python Alphabet Pattern 7
Continue to Program 7 for the next alphabet pattern in Python.
Patterns that shift their start index each row are a great way to practice translating math bounds into loop ranges.
10 people found this page helpful
