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.
Approach
How to Solve It
For each row: pad, print A up to the peak, then mirror back from peak - 1.
Method
Idea
Best for
Three print stages
Spaces, ascend, descend with end=""
Learning, interviews, exams
String halves
Build left + right, then one print
Debugging; 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
Goal
Pattern
Walk rows
for r in range(rows):
Leading pads
print(" " * (rows - 1 - r), end="")
Peak letter
peak = ord("A") + r
Ascend
for code in range(base, peak + 1):
Descend
for code in range(peak - 1, base - 1, -1):
Skip double peak
Descend starts at peak - 1
End the row
print()
vs Program 18
Same letters; Program 32 adds centering spaces
Printing Letters vs Starting a New Line
API
Effect
Use for
print(..., end="")
Stays on the same row
Each space and each letter
print()
Ends the current row
After pads + ascend + descend
Print cells without a newline, then end the row once.
Try it
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 result5 rows · peak E
A
ABA
ABCBA
ABCDCBA
ABCDEDCBA
Trace
Worked Walkthrough — rows = 3
Pads shrink while the palindrome grows. Descend always skips the peak.
r
Pads / halves
Printed row
0
2 spaces + A + (empty)
A
1
1 space + AB + A
ABA
2
0 spaces + ABC + BA
ABCBA
Row r prints 2r+1 letters → total work O(n²).
Code
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()
Output
A
ABA
ABCBA
ABCDCBA
ABCDEDCBA
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()
Output (when user enters 3)
A
ABA
ABCBA
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)
Output
A
ABA
ABCBA
ABCDCBA
ABCDEDCBA
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.
Analysis
Time and Space Complexity
Program
Time
Extra 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.
Remember
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.