Alphabet Rotation Pattern in Python

What You'll Learn
This program prints a wrap-around alphabet pattern. Each row starts at a later letter and still prints a full row by wrapping back to A.
⭐ Pattern Output
When you run the program with rows = 5:
ABCDE\nBCDEA\nCDEBA\nDECBA\nEDCBAComplete Python Program
Fixed rows = 5 version (same output as the reference; using ord()/chr()):
rows = 5
rows = max(1, min(rows, 26))
base = ord('A')
top = base + rows - 1
for i in range(base, top + 1): # A..E
for j in range(i, top + 1): # i..E (increasing)
print(chr(j), end="")
for k in range(i - 1, base - 1, -1): # (i-1)..A (decreasing)
print(chr(k), end="")
print()🧠 How It Works
Row start letter
The outer loop picks i as the starting letter of the row: A, then B, then C, …
First half: i to top
for j in range(i, top + 1) prints from the row start to the top letter (e.g. B..E).
Second half: wrap back to A
for k in range(i - 1, base - 1, -1) prints the remaining letters by counting down to A, so the row length stays constant.
Rotated rows
Each row prints rows letters, and there are rows rows, so runtime is O(n²) and extra space is O(1).
Variation — User Input Version
Read rows from input (clamped to 26):
rows = int(input("Enter number of rows (max 26): "))
rows = max(1, min(rows, 26))
base = ord('A')
top = base + rows - 1
for i in range(base, top + 1):
for j in range(i, top + 1):
print(chr(j), end="")
for k in range(i - 1, base - 1, -1):
print(chr(k), end="")
print()💡 Tips for Enhancement
Try These
- Print with spaces between letters for readability
- Use lowercase by starting from
ord('a') - Try wrapping with modulo to support more than 26 letters
Avoid
- Using magic numbers like 65 or 70 directly
- Letting rows exceed 26 without wrapping
Key Takeaways
Row start moves A → B → C …
First half prints up to the top letter; second half wraps back down to A.
Each row prints n letters for n rows — O(n²).
❓ Frequently Asked Questions
end=\"\" to end=\" \" for the letter prints (and strip() the line if needed).i reaches the top letter, the forward loop prints only that letter, and the wrap loop prints the rest down to A, producing a full reverse row.n rows because each row prints ~n characters.Next: Python Alphabet Pattern 27
Continue to Program 27 for the next alphabet pattern in Python.
10 people found this page helpful
