Mixed Alphabet Pattern in Python

Beginner
⏱️ 6 min read
📚 Updated: Aug 2025
🎯 2 Code Examples
down then up

What You'll Learn

This program prints rows like ABCDE, BABCD, CBABC, DCBAB, EDCBA by combining a descending prefix down to A and an ascending suffix.

⭐ Pattern Output

When you run the program with rows = 5:

Output
ABCDE
BABCD
CBABC
DCBAB
EDCBA
1

Complete Python Program

Fixed rows = 5 version (no magic ASCII values like 64):

Python
rows = 5
rows = max(1, min(rows, 26))

base = ord('A')
top = base + rows - 1

for r in range(rows):  # 0..4
    start = base + r          # A, B, C, D, E
    end = top - r             # E, D, C, B, A

    # Descending prefix: start..A
    for code in range(start, base - 1, -1):
        print(chr(code), end="")

    # Ascending suffix: B..end (skip A)
    for code in range(base + 1, end + 1):
        print(chr(code), end="")

    print()

🧠 How It Works

1

Start and end letters per row

For row index r, start = A + r moves forward, while end = top - r moves backward (E, D, C, …).

Setup
2

Descending prefix down to A

for code in range(start, A-1, -1) prints start down to A. Example: for start = C it prints CBA.

Down
3

Ascending suffix from B

Then we print from B up to end. Starting from B avoids duplicating A in the middle.

Up
=

Shrinking suffix

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

2

Variation — User Input Version

Read rows from input (clamped to 26):

Python
rows = int(input("Enter number of rows (max 26): "))
rows = max(1, min(rows, 26))

base = ord('A')
top = base + rows - 1

for r in range(rows):
    start = base + r
    end = top - r
    for code in range(start, base - 1, -1):
        print(chr(code), end="")
    for code in range(base + 1, end + 1):
        print(chr(code), end="")
    print()

💡 Tips for Enhancement

Try These

  • Add spaces between letters for readability
  • Use lowercase by setting base = ord('a')
  • Center the output with leading spaces per row

Avoid

  • Printing A twice in the middle (start the suffix at B)
  • Hardcoding ASCII values like 64 or 70

Key Takeaways

1

Prefix prints from the row letter down to A.

2

Suffix prints from B up to a shrinking end letter.

3

Overall runtime is O(n²).

❓ Frequently Asked Questions

Yes. Print each letter with end=\" \" or build the row in a list and use \" \".join(letters).
Because end = top - r decreases as r increases, shrinking the ascending part.
It’s O(n²) for n rows.

Next: Python Alphabet Pattern 31

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

Program 31 →

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