Python Alphabet Pattern (Decrease then Increase)

Beginner
7 min read
Updated: Sep 2026
3 programs
Live preview

What Is This Pattern?

Mixed alphabet rows keep a fixed width: each row starts with a growing descending prefix, then fills the rest with a shrinking ascending suffix — no cyclic wrap.

Remember
Rule: prefix start..A  +  suffix B..end
      start = A+r,  end = top−r  →  width = rows

ABCDE
BABCD
CBABC
DCBAB
EDCBA     ← 5 rows, width 5

Row 1 is pure ascending (ABCDE); the last row is pure descending (EDCBA). Compare Program 26 (rotating wrap) and Program 29 (layered diamond).

How to Solve It

For each row index r, print start..A descending, then B..end ascending.

MethodIdeaBest for
Two loops inlineDescending prefix, then ascending suffix with end=""Learning, interviews, exams
List + joinAppend letters, then ''.join(row)Debugging; inspect the row before print

Pseudocode

Pseudocode
base = 'A'
top = base + rows - 1
for r from 0 to rows - 1:
    start = base + r
    end = top - r
    for code from start down to base:   // prefix
        print code
    for code from base+1 to end:        // suffix (skip A)
        print code
    print newline

Cheat sheet

GoalPattern
Top lettertop = ord("A") + rows - 1
Walk rowsfor r in range(rows):
Boundsstart = base + r, end = top - r
Descending prefixfor code in range(start, base - 1, -1):
Ascending suffixfor code in range(base + 1, end + 1): — start at B
WidthAlways rows letters per line
End the rowprint()
vs Program 26No wrap — BABCD, not BCDEA

Printing Letters vs Starting a New Line

APIEffectUse for
print(chr(code), end="")Stays on the same rowEach letter in prefix and suffix
print()Ends the current rowAfter both inner loops

Print letters without a newline, then end the row once.

Live Preview

Change the row count and the mixed alphabet grid updates instantly — every line stays the same width.

Whole numbers from 1 to 10. Size 5 means letters A–E and width 5. Tap a chip or type a value — the preview redraws as you go.

Live result 5 rows · width 5
ABCDE
BABCD
CBABC
DCBAB
EDCBA

Worked Walkthrough — rows = 3

top = C. Trace prefix and suffix for each row index r.

rPrefix / suffixPrinted row
0A + BCABC
1BA + CBAC
2CBA + (empty)CBA

Each of n rows prints n letters → O(n²).

Python Programs

Three complete programs: fixed height, row-count input(), and a list-join style. Use View Output for sample results.

Example 1 — Fixed rows = 5

Descending prefix to A, then ascending suffix from B to a shrinking end.

Python
rows = 5
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()

How It Works

1. Bounds. start = base + r grows the prefix; end = top - r shrinks the suffix.

2. Prefix. Print start..A descending. On row 0 that is just A.

3. Suffix. Print B..end. When end < B (last row), the suffix is empty → EDCBA.

Example 2 — Row Count Input

Read how many rows to print. Cap at 26 so letters stay in A–Z.

Python
raw = input("Enter number of rows (1-26): ").strip()
try:
    rows = int(raw)
except ValueError:
    print("Please enter a whole number.")
    raise SystemExit(1)

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

How It Works

1. Validate rows. Parse an integer, then clamp to 1..26.

2. Same dual loops. Three rows use A–C with width 3 — row 2 is BAC, not cyclic BCA.

Example 3 — List Join

Collect letters in a list, then print with ''.join(row) — same shape, easier to inspect.

Python
rows = 5
base = ord("A")
top = base + rows - 1

for r in range(rows):
    start = base + r
    end = top - r
    row = []

    for code in range(start, base - 1, -1):
        row.append(chr(code))

    for code in range(base + 1, end + 1):
        row.append(chr(code))

    print("".join(row))

How It Works

1. Build then print. Both loops append to row instead of printing immediately.

2. Join once. "".join(row) concatenates without spaces — identical output to Example 1.

Edge Cases & Pitfalls

Check these before calling the solution done.

double A

Suffix starts at A

Starting the second loop at base duplicates A (e.g. BAAC). Keep range(base + 1, end + 1).

wrap habit

Confusing with Program 26

This is not a rotate: row 2 is BABCD, not BCDEA.

newline early

Bare print() inside a half

Use end="" for letters; call bare print() only after both loops.

rows > 26

Top past Z

Cap rows at 26 so ord("A") + rows - 1 stays in A–Z.

rows = 1

Single A

Prefix is A; suffix is empty because end < B.

Bad input

Non-integer rows

Catch ValueError from int(...) before computing top.

Time and Space Complexity

ProgramTimeExtra space
Inline print (Examples 1–2)O(n²)O(1)
List join (Example 3)O(n²)O(n) per row for the list

There are n rows and each prints n letters, so total work is quadratic.

Key Takeaways

  • Two passes: descending start..A, then ascending B..end.
  • Fixed width: as the prefix grows, the suffix shrinks — every row has n letters.
  • Skip A: suffix starts at B so the join does not print AA.
  • Next step: Program 31 draws an alphabet X with diagonals.

One line: for each r, print start..A down then B..end up with start = A+r and end = top−r.

Frequently Asked Questions

Each row combines a descending prefix from the row start letter down to A with an ascending suffix from B up to a shrinking end letter. Row 1 is ABCDE; row 2 is BABCD; row 5 is EDCBA.
Program 26 rotates fixed-width rows with wrap-around (ABCDE, BCDEA). Here the prefix descends and the suffix ascends without cyclic wrap — BABCD, not BCDEA.
The first loop prints the descending prefix from start down to A. The second prints the ascending suffix from B to end — skipping A avoids duplicating the letter at the join point.
A is already printed as the last character of the descending prefix (except row 1 where start is A). Starting the suffix at B prevents AA in rows like BAC.
Each row uses letters A through the nth letter. With rows > 26 you would need characters beyond Z unless you define a wrap policy.
print(chr(code), end="") keeps letters on the same row. print() ends the row after both inner loops finish.
O(n²) where n is rows. There are n rows and each row prints n characters.
Use try/except ValueError around int(input()), then clamp rows between 1 and 26.

Did you know?

Each row uses two passes: a descending prefix from the row start letter down to A, then an ascending suffix from B up to end = top - r. Row 1 is pure ascending ABCDE; the last row is pure descending EDCBA.

Next: Alphabet X Pattern

Print letters on the two diagonals of a square grid to form an X.

Program 31 tutorial →

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.

12 people found this page helpful