Python Symmetric Alphabet Pyramid Pattern

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

What Is This Pattern?

A centered alphabet palindrome pyramid prints mirrored letter rows (A, ABA, ABCBA…) with leading spaces so the peak sits in the middle.

Remember
Rule: (rows-1-r) spaces + A..peak + (peak-1)..A

    A
   ABA
  ABCBA
 ABCDCBA
ABCDEDCBA     ← 5 rows

Compare Program 18 (same palindromes, left-aligned) and Program 31 (alphabet X). Program 33 widens hollow letter rows instead.

How to Solve It

For each row: pad, print A up to the peak, then mirror back from peak - 1.

MethodIdeaBest for
Three print stagesSpaces, ascend, descend with end=""Learning, interviews, exams
String halvesBuild left + right, then one printDebugging; inspect each half

Pseudocode

Pseudocode
base = 'A'
for r from 0 to rows - 1:
    print (rows - 1 - r) spaces
    peak = base + r
    for code from base to peak:       // ascend
        print code
    for code from peak-1 down to base: // descend (skip peak)
        print code
    print newline

Cheat sheet

GoalPattern
Walk rowsfor r in range(rows):
Leading padsprint(" " * (rows - 1 - r), end="")
Peak letterpeak = ord("A") + r
Ascendfor code in range(base, peak + 1):
Descendfor code in range(peak - 1, base - 1, -1):
Skip double peakDescend starts at peak - 1
End the rowprint()
vs Program 18Same letters; Program 32 adds centering spaces

Printing Letters vs Starting a New Line

APIEffectUse for
print(..., end="")Stays on the same rowEach space and each letter
print()Ends the current rowAfter pads + ascend + descend

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

Live Preview

Change the row count and the centered palindrome pyramid updates instantly — including pad count on the first row.

Whole numbers from 1 to 10. Size 5 means peak letter E on the bottom row. Tap a chip or type a value — the preview redraws as you go.

Live result 5 rows · peak E
    A
   ABA
  ABCBA
 ABCDCBA
ABCDEDCBA

Worked Walkthrough — rows = 3

Pads shrink while the palindrome grows. Descend always skips the peak.

rPads / halvesPrinted row
02 spaces + A + (empty)A
11 space + AB + AABA
20 spaces + ABC + BAABCBA

Row r prints 2r+1 letters → total work O(n²).

Python Programs

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

Example 1 — Fixed rows = 5

Pad, ascend A..peak, then descend peak-1..A.

Python
rows = 5
base = ord("A")

for r in range(rows):
    print(" " * (rows - 1 - r), end="")

    peak = base + r

    for code in range(base, peak + 1):
        print(chr(code), end="")

    for code in range(peak - 1, base - 1, -1):
        print(chr(code), end="")

    print()

How It Works

1. Center. " " * (rows - 1 - r) pads the left so the peak lines up.

2. Ascend. Print A through peak = base + r (E on the last row).

3. Descend. Mirror from peak - 1 back to A — avoids a doubled center letter.

Example 2 — Row Count Input

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

Python
raw = input("Enter number of rows (max 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")

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

How It Works

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

2. Same three stages. Three rows produce A, ABA, and ABCBA with 2, 1, and 0 leading spaces.

Example 3 — String Halves

Build ascend and descend as strings, then print pads + left + right once.

Python
rows = 5
base = ord("A")

for r in range(rows):
    peak = base + r
    left = "".join(chr(c) for c in range(base, peak + 1))
    right = "".join(chr(c) for c in range(peak - 1, base - 1, -1))
    print(" " * (rows - 1 - r) + left + right)

How It Works

1. Build halves. left is A..peak; right is (peak-1)..A.

2. One print. Concatenate pads + left + right — identical shape to Example 1, easier to inspect.

Edge Cases & Pitfalls

Check these before calling the solution done.

double peak

Descend starts at peak

Starting at peak prints ABCCBA. Keep range(peak - 1, ...).

no pads

Missing leading spaces

Without (rows - 1 - r) pads you get Program 18’s left-aligned triangle.

newline early

Bare print() mid-row

Use end="" for pads and letters; call bare print() only after all three stages.

rows > 26

Peak past Z

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

rows = 1

Single A

No pads, ascend prints A, descend is empty — a good sanity check.

Bad input

Non-integer rows

Catch ValueError from int(...) before the outer loop.

Time and Space Complexity

ProgramTimeExtra space
Inline print (Examples 1–2)O(n²)O(1)
String halves (Example 3)O(n²)O(n) per row for the strings

Row r prints 2r+1 letters (plus pads). Summing over n rows is quadratic.

Key Takeaways

  • Three stages: pads, ascend A..peak, descend (peak-1)..A.
  • Center once: descend starts at peak - 1 so you get ABCBA, not ABCCBA.
  • Pads: (rows - 1 - r) spaces keep the pyramid centered.
  • Next step: Program 33 prints a widening hollow alphabet triangle.

One line: for each row, print pads, then A..peak, then (peak-1)..A.

Frequently Asked Questions

Each row is a palindrome of letters (A, ABA, ABCBA, ...) padded with leading spaces so the pyramid is centered. Row r prints (rows-1-r) spaces, then A..peak, then (peak-1)..A.
The ascend loop already prints the peak letter. Starting the mirror at peak-1 avoids duplicating the center character — ABCBA not ABCCBA.
Print (rows - 1 - r) spaces before the letters. Row 0 gets the most spaces; the bottom row gets zero.
Program 18 prints the same palindrome rows left-aligned with no leading spaces. Program 32 adds (rows-1-r) spaces to center the pyramid.
Each row peak is chr(ord('A') + r). With rows > 26 you would need characters beyond Z unless you define a wrap or error policy.
print(chr(code), end="") keeps each letter on the same row. print() ends the row after spaces, ascend, and descend finish.
O(n²) where n is rows. Row r prints about 2r+1 letters plus spaces; the sum of odd lengths is n².
Use try/except ValueError around int(input()), then clamp rows between 1 and 26.

Did you know?

Row r prints (rows - 1 - r) leading spaces, then letters from A up to the peak chr(ord('A') + r), then back down to A starting at peak - 1 so the peak is not duplicated.

Next: Widening Alphabet Triangle

Print hollow rows where each letter sits farther apart as the triangle grows.

Program 33 tutorial →

CODETOFUN NEWS

Stay Ahead in Tech

Get the latest IT news, programming updates, and technology trends directly on your phone.

Your daily dose of tech, straight to your feed.

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