Python Palindromic Alphabet Pyramid Pattern (Center A)

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

What Is This Pattern?

A palindromic alphabet pyramid prints mirrored letter rows that grow around a single center A: each line reads the same forward and backward.

Remember
Rule: for peak i from 'A' to top,
      print i..B (descending), then A..i (ascending)

A
BAB
CBABC
DCBABCD
EDCBABCDE     ← 5 rows (top = 'E')

The descending wing stops before A so the center letter is not printed twice. Compare with Program 18 (different wing order) and add pads (or see Program 16) if you want the shape centered.

How to Solve It

Two ways to emit the same palindrome — classic ord/chr loops, then optionally space each letter for a wider look.

MethodIdeaBest for
Descend + ascendLeft wing i..B; right wing A..iLearning, interviews, exams
Spaced lettersSame wings; print(chr(j), end=" ")Clearer demos in dense terminals

Pseudocode

Pseudocode
for i from 'A' to top:
    for j from i down to 'B':
        print j (no newline)
    for j from 'A' to i:
        print j (no newline)
    print newline

Cheat sheet

GoalPattern
Pick the peakfor i in range(ord("A"), ord("E") + 1):
Left wingfor j in range(i, ord("A"), -1): print(chr(j), end="")
Center + rightfor j in range(ord("A"), i + 1): print(chr(j), end="")
End the rowprint()
Row length2 * (i - ord("A")) + 1 (odd widths)
Peak from rowspeak = ord("A") + r
Avoid double AUse range(i, ord("A"), -1) — stops before A

Printing Letters vs Starting a New Line

APIEffectUse for
print(..., end="")Stays on the same lineEach letter on both wings
print()Ends the current lineAfter both inner loops

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

Live Preview

Change the height and the palindrome updates instantly — including the letter total (n²).

Whole numbers from 1 to 10. Tap a chip or type a value — the preview redraws as you go.

Live result 5 rows · 25 letters
A
BAB
CBABC
DCBABCD
EDCBABCDE

Worked Walkthrough — A–E

Trace each row’s left wing, center + right wing, and full line.

iLeft (i..B)Right (A..i)Printed row
A(empty)AA
BBABBAB
CCBABCCBABC
DDCBABCDDCBABCD
EEDCBABCDEEDCBABCDE

Lengths: 1 + 3 + 5 + 7 + 9 = 25 = 5². That odd-number sum is why time is O(n²).

Python Programs

Three complete programs: fixed A–E, input() row-count, and a spaced-letter variant. Use View Output to reveal sample results.

Example 1 — Fixed A–E

Two loops per row: descending (i..B), then ascending (A..i). The descending loop stops before A.

Python
for i in range(ord("A"), ord("E") + 1):
    for j in range(i, ord("A"), -1):
        print(chr(j), end="")

    for j in range(ord("A"), i + 1):
        print(chr(j), end="")

    print()

How It Works

1. Outer loop picks the peak. i runs from ord("A") to ord("E") — one row per peak letter.

2. Left wing descends. range(i, ord("A"), -1) prints down to B and stops before A.

3. Center + right ascend. Print from A through i — that supplies the single center A.

4. Break the line. Bare print() after both wings starts the next row.

When i is C, the left wing prints CB and the right wing prints ABC → CBABC.

Example 2 — Row Count Input

Cap at 26 for A–Z. Prefer try/except ValueError in real apps.

Python
n = int(input("Enter number of rows (1..26): "))
if n < 1:
    raise SystemExit
if n > 26:
    n = 26

for r in range(n):
    peak = ord("A") + r

    for ch in range(peak, ord("A"), -1):
        print(chr(ch), end="")

    for ch in range(ord("A"), peak + 1):
        print(chr(ch), end="")

    print()

How It Works

1. Prompt and clamp. Read a row count and keep it in 1–26 so peaks stay in A–Z.

2. Map index to peak. Row r uses peak = ord("A") + r.

3. Same wing core. Descend/ascend rules match Example 1 — only the number of peaks changes.

4. Safer input tip. Prefer:

Safer input
try:
    n = int(input("Enter number of rows (1..26): "))
except ValueError:
    print("Enter a whole number from 1 to 26.")
    raise SystemExit

if n < 1 or n > 26:
    print("Enter a whole number from 1 to 26.")
    raise SystemExit

Example 3 — Spaced Letters

Same palindrome with a space after each letter for a wider look.

Python
for i in range(ord("A"), ord("E") + 1):
    for j in range(i, ord("A"), -1):
        print(chr(j), end=" ")

    for j in range(ord("A"), i + 1):
        print(chr(j), end=" ")

    print()

How It Works

1. Same wings. Still descend i..B, then ascend A..i.

2. Format only. end=" " adds a trailing space after each letter.

3. Optional polish. Trim the final space per row if you need a clean line end.

Edge Cases & Pitfalls

Check these before calling the solution done.

Through A

Duplicate center A

If the descending loop goes through A (e.g. range(i, ord("A") - 1, -1)), you get BAAB or CBABBC. Keep range(i, ord("A"), -1).

Wing order

Wrong palindrome style

Ascending first then descending gives Program 18-style rows (e.g. ABCBA) instead of CBABC.

print() inside

Column of letters

If bare print() (or default newline) is inside either wing loop, each letter lands on its own line. Use end="" for letters; call print() only after both loops.

n = 1

Single A

Left wing never runs; output is just A. A good sanity check.

n > 26

Past Z

Clamp or reject so peaks stay in A–Z.

Bad input

Use try/except

int(input()) raises on letters — prefer try/except ValueError and require 1–26.

Time and Space Complexity

ProgramTimeExtra space
Descend + ascend (Examples 1–2)O(n²)O(1)
Spaced letters (Example 3)O(n²)O(1)

Total letters = 1 + 3 + … + (2n - 1) = n². Spaced output prints the same letters plus spaces — still quadratic in n.

Key Takeaways

  • Rule: for peak i, print i..B then A..i.
  • Single center A: use range(i, ord("A"), -1) so the descending wing stops before A.
  • Break the row: call print() only after both wings.
  • Complexity: O(n²) time from n² letters; O(1) extra space.

One line: for each peak i, print descending i..B, then ascending A..i, then print().

Frequently Asked Questions

The first loop prints descending letters from the row peak down to B. The second prints ascending from A through i. Together they mirror around a single A.
range(i, ord('A'), -1) stops before A. The forward loop already prints A, so stopping the descending loop before A avoids duplicating the center character.
The descending loop stops before A, and the ascending loop starts at A. That prevents printing A twice at the join.
For row letter i, length is 2*(i-ord('A'))+1, so rows grow as 1, 3, 5, 7, 9 for A..E.
Change the outer loop upper bound from ord('E') to your target letter (like ord('H')), or use the row-count input() variant.
Program 18 typically builds A..peak then peak-1..A. This pattern descends to B first, then ascends A..i, still mirroring around a single A.
O(n²) for n rows because total printed characters are 1+3+...+(2n-1) = n².
No — the classic version is left-aligned with no leading spaces. Add pad spaces (or see Program 16) if you want a centered pyramid.

Did you know?

Outer i runs A.. E. First inner loop prints the left wing (i down to B) by stopping at j > ord('A'). Second inner loop prints A.. i. Because the reverse loop stops before A, the center A is not duplicated.

Next: Sequential Decreasing

Move from mirrored wings to a running letter stream with shrinking row lengths.

Program 25 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