Alphabet Pattern (E, DE, CDE, BCDE, ABCDE) in Python

What You'll Learn
This Python program prints an alphabet pattern where each row ends at a fixed top letter (E for rows = 5) while the starting letter moves backward 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:
E
DE
CDE
BCDE
ABCDEComplete Python Program
Fixed rows = 5 version (computed top letter):
rows = 5
top = ord('A') + rows - 1 # 'E' when rows = 5
for i in range(1, rows + 1):
start = top - (i - 1)
for code in range(start, top + 1):
print(chr(code), end="")
print()🧠 How It Works
Pick the top letter
top = ord('A') + rows - 1 sets the fixed ending letter for every row. For rows = 5, it becomes ord('E').
Compute the row start
For row i, we start at start = top - (i - 1). That gives starts E, D, C, B, A for rows 1 through 5.
Print ascending to the top
range(start, top + 1) prints letters from the row’s start up to the fixed top letter. Using end="" keeps letters on the same line.
Expanding from the left
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))
top = ord('A') + rows - 1
for i in range(1, rows + 1):
start = top - (i - 1)
for code in range(start, top + 1):
print(chr(code), end="")
print()💡 Tips for Enhancement
Try These
- Change the base letter to print lowercase patterns
- Add spaces between letters for readability
- Reverse each row to get
ED,EDC, ... - Try centering the output for a pyramid effect
Avoid
- Hardcoding ASCII values like
70or69 - Allowing
rows > 26without defining wrap-around behavior - Forgetting a newline after each row
- Mixing tabs/spaces in Python snippets
Key Takeaways
Fix a top letter (top) and compute per-row start.
Row i prints from start through top.
Total printed letters are \(n(n+1)/2\), so runtime is O(n²).
Use ord() and chr() for clean letter arithmetic.
Clamp user input to 26 to stay within A–Z.
❓ Frequently Asked Questions
top = ord('Z') and clamp rows so the computed start does not go before A.range(start, top + 1) includes the top letter.n rows, because the program prints 1+2+…+n letters in total.Next: Python Alphabet Pattern 4
Continue to Program 4 for the next alphabet pattern in Python.
In Python, you can build each row as a string too (e.g., with ''.join(...)), but printing inside nested loops is perfect for learning.
10 people found this page helpful
