Alphabet Rotation Pattern in Python

Beginner
⏱️ 6 min read
📚 Updated: Aug 2025
🎯 2 Code Examples
wrap-around row

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:

Output
ABCDE\nBCDEA\nCDEBA\nDECBA\nEDCBA
1

Complete Python Program

Fixed rows = 5 version (same output as the reference; using ord()/chr()):

\n Python
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

1

Row start letter

The outer loop picks i as the starting letter of the row: A, then B, then C, …

Outer
2

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).

Forward
3

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.

Wrap
=

Rotated rows

Each row prints rows letters, and there are rows rows, so runtime is O(n²) and extra space is O(1).

2

Variation — User Input Version

Read rows from input (clamped to 26):

\n Python
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

1

Row start moves A → B → C …

2

First half prints up to the top letter; second half wraps back down to A.

3

Each row prints n letters for n rows — O(n²).

❓ Frequently Asked Questions

Yes. Change end=\"\" to end=\" \" for the letter prints (and strip() the line if needed).
When 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.
It’s O(n²) for n rows because each row prints ~n characters.

Next: Python Alphabet Pattern 27

Continue to Program 27 for the next alphabet pattern in Python.

Program 27 →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

10 people found this page helpful