Palindrome Rule
A..peak..A
Each row ascends from A to the peak, then descends from peak - 1 back to A.

Each row is a centered palindrome of letters: row 0 prints A, row 1 prints ABA, row 2 prints ABCBA, until the bottom row shows ABCDEDCBA for five rows. Leading spaces (rows - 1 - r) center the pyramid; ascend and descend loops mirror letters without duplicating the peak. Compare with Program 18 (left-aligned palindrome pyramid). Includes a live preview, worked Python examples, edge cases, and complexity.
A..peak..A
Each row ascends from A to the peak, then descends from peak - 1 back to A.
rows - 1 - r
print(" " * (rows - 1 - r), end="") centers each palindrome row under the apex.
A..peak
for code in range(base, peak + 1): prints letters up to and including the peak.
peak-1..A
for code in range(peak - 1, base - 1, -1): mirrors back without repeating the peak.
1–26 rows
Pick a row count and draw the centered palindrome pyramid in the browser instantly.
Complexity
Row r prints 2r + 1 letters; total letters = n² ≈ O(n²); extra memory stays O(1).
A centered alphabet palindrome pyramid prints each row as a mirror string of letters, padded with leading spaces so the shape is centered. Row 0 prints A; each next row adds one more letter to the peak and mirrors back — ABA, ABCBA, and so on.
In Python you solve it with an outer loop over row index r, a space prefix, two inner loops for ascend and descend, and ord()/chr() — or build left and right strings and concatenate for clarity.
It combines three classic pattern skills — centering with spaces, ascending sequences, and mirror loops that skip the peak — the same building blocks used in diamonds, hollow pyramids, and symmetric ASCII art. Compare with Program 18 to see how centering transforms the same palindrome rows.
" " * (rows - 1 - r) — row 0 gets rows - 1 spaces; bottom row gets none.
range(base, peak + 1) — prints A through the row peak letter.
range(peak - 1, base - 1, -1) — mirrors back starting below the peak.
Descend starts at peak - 1 so ABCBA stays a true palindrome.
In short: set base = ord('A'), loop r from 0 to rows - 1, print (rows - 1 - r) spaces, ascend A..peak, descend (peak-1)..A, then print() for the newline.
Given a positive integer rows, print a centered pyramid of rows lines. Row r prints (rows - 1 - r) leading spaces, then letters from A up to chr(ord('A') + r), then back down from peak - 1 to A.
# First 5 rows (centered)
A
ABA
ABCBA
ABCDCBA
ABCDEDCBA | Item | Type | Description |
|---|---|---|
rows | int | Number of rows (peak letter runs A through the rows-th letter). Clamp to 1–26 for A–Z demos. |
| Printed output | text | Centered palindrome pyramid: each row is a mirror string with leading spaces — widest row has 2*rows - 1 letters. |
base = ord('A')
for r from 0 to rows-1:
print (rows-1-r) spaces
peak = base + r
for code from base to peak: print chr(code)
for code from peak-1 down to base: print chr(code)
print newline | Approach | Idea | Best for |
|---|---|---|
| Ascend + descend loops | Two for code in range(...) loops with print(..., end="") | Learning mirror loops and peak-off-by-one |
| Left/right strings | ''.join(chr(c) for c in range(...)) then concatenate | Clearer debugging and row inspection |
| Program 18 contrast | See Program 18 (left-aligned palindrome) | Same palindrome logic without centering spaces |
| Goal | Pattern |
|---|---|
| Leading spaces | print(" " * (rows - 1 - r), end="") |
| Outer loop (row index) | for r in range(rows): |
| Peak letter code | peak = base + r |
| Ascend loop | for code in range(base, peak + 1): print(chr(code), end="") |
| Descend loop | for code in range(peak - 1, base - 1, -1): print(chr(code), end="") |
| End the row | print() |
| String variant | left + right with ''.join(chr(c) for c in range(...)) |
Three parts of every row — pick the mental model that clicks for you.
rows - 1 - r
centers rowTop row gets the most padding; bottom row aligns flush left before letters.
base..peak
A, AB, ABC...Prints letters from A up to and including the row peak.
peak-1..base
mirror halfWalks back down from one below the peak — avoids duplicating the center letter.
Reach for centered palindrome pyramids when teaching mirror loops, spacing, and symmetric row building after diagonal patterns.
Program 31 places letters on two diagonals forming an X. This pattern builds full palindrome strings centered with leading spaces.
Master the peak - 1 start index before tackling full diamonds and hollow shapes.
The (rows - 1 - r) space formula appears in centered stars, numbers, and diamond patterns.
Next pattern widens letter pairs — another symmetric triangle variation.
This is a console teaching pattern — not how you build modern app screens.
Key benefit: one program that combines centering spaces with mirror loops — the same two skills used in diamond patterns, hollow pyramids, and symmetric ASCII art far beyond alphabet demos.
Choose a row count between 1 and 26 and draw the centered alphabet palindrome pyramid in the browser.
Three complete Python programs — fixed five rows with ascend/descend loops, console input with the same logic, and a left/right string variant for clarity. Click View Output to reveal sample console results.
Print five rows of the centered alphabet palindrome pyramid with leading spaces and mirror loops.
rows = 5Hard-coded height — ideal for first demos and screenshots.
rows = 5
rows = max(1, min(rows, 26))
base = ord('A')
for r in range(rows): # 0..4
# Centering spaces
print(" " * (rows - 1 - r), end="")
peak = base + r
# Ascend: A..peak
for code in range(base, peak + 1):
print(chr(code), end="")
# Descend: (peak-1)..A
for code in range(peak - 1, base - 1, -1):
print(chr(code), end="")
print() The outer loop walks row index r from 0 to 4. For each row, print(" " * (rows - 1 - r), end="") centers the palindrome, then peak = base + r sets the row peak letter. The ascend loop prints A through the peak; the descend loop mirrors from peak - 1 back to A without duplicating the center. Row 0 prints only A with four leading spaces; row 4 prints the full ABCDEDCBA with no spaces.
Let the user choose the height at runtime.
Read rows and clamp to 1–26. Wrap int(input()) in try/except ValueError in real apps.
rows = int(input("Enter number of rows (max 26): "))
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() Same centered palindrome core as Example 1; only the row count comes from input. Three rows produce A, ABA, and ABCBA with 2, 1, and 0 leading spaces respectively.
Build left and right halves as strings, then print spaces + left + right.
Build ascend and descend halves with generator expressions — same logic, easier row inspection.
rows = 5
rows = max(1, min(rows, 26))
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) left builds the ascend half and right builds the descend half as strings. Concatenating with leading spaces produces identical output to Examples 1 and 2, but you can inspect left and right separately during debugging.
Clamp rows, then set base = ord('A') for the alphabet starting point.
print(" " * (rows - 1 - r), end="") centers row r before any letters.
Ascend A..peak, then descend (peak-1)..A with print(chr(code), end="") in both loops.
print() ends the row after spaces and both letter loops finish; the outer loop advances r.
Total letters: 1 + 3 + 5 + ... + (2n-1) = n² — O(n²) time, O(1) extra memory (loop version).
rows = 5Trace each outer-loop value of r and see how leading spaces, peak letter, ascend, and descend produce each centered palindrome row.
r | peak | spaces | ascend | descend | full row |
|---|---|---|---|---|---|
| 0 | A | 4 | A | (none) | A |
| 1 | B | 3 | AB | A | ABA |
| 2 | C | 2 | ABC | BA | ABCBA |
| 3 | D | 1 | ABCD | CBA | ABCDCBA |
| 4 | E | 0 | ABCDE | DCBA | ABCDEDCBA |
Highlight rows: r = 0 (4 spaces, peak A, A only), r = 2 (2 spaces, peak C, ABCBA), r = 4 (0 spaces, peak E, ABCDEDCBA). Total letters printed: 1 + 3 + 5 + 7 + 9 = 25 = rows² for rows = 5.
Where centered palindrome pyramids show up beyond the homework prompt.
Program 31 uses diagonal columns for an X shape. This pattern builds full palindrome strings centered with spaces.
Example: compare diagonal X grid vs centered ABCBA rows side by side.
Reinforce the peak - 1 descend start before tackling diamonds and hollow pyramids.
Example: trace row 2 (r=2) on paper: spaces=2, ascend=ABC, descend=BA.
Same palindrome rows without centering — see Program 18.
Example: add print(" " * (rows - 1 - r), end="") to Program 18 to get this shape.
Mirror the pyramid downward to close a full alphabet diamond.
Example: after the top half, loop r from rows-2 down to 0 with the same row logic.
Sum of odd row lengths makes O(n²) concrete for beginners.
Example: 5 rows → 25 letters printed (1+3+5+7+9).
Classic nested-loop question that tests mirror logic and centering spaces.
Example: explain why descend starts at peak - 1 without running code.
Pro Tip: say “spaces, then up to peak, then down from peak minus one” before coding — that story prevents duplicated peaks and wrong indentation.
Why this pattern earns a spot after the alphabet X pattern from Program 31.
Ascend then descend with peak - 1 — a pattern reused in diamonds and hollow shapes.
Leading spaces create a visually balanced pyramid — every row aligns under the apex.
Direct print loops for learning; left/right string variant for clearer debugging.
Streaming output needs no storage beyond loop counters (string variant uses O(r) per row).
Pro Tip: when row 0 prints only A, the descend loop range is empty — that is correct, not a bug.
Small habits that keep centered palindrome pyramid code clean.
Use peak = base + r — keeps ascend and descend loops readable.
int(input()) in try/exceptAvoid crashes when the user types letters instead of a number.
rows = max(1, min(rows, 26)) keeps demos inside A–Z.
print(" " * (rows - 1 - r), end="") must run before the ascend loop each row.
Trace A, ABA, ABCBA with 2, 1, 0 spaces on paper before coding larger demos.
Pro Tip: if the pyramid leans left, check the space count — it should be rows - 1 - r, not r.
Mistakes that commonly break centered alphabet palindrome pyramids.
Starting descend at peak instead of peak - 1 prints ABCCBA — a doubled center letter.
→ Use for code in range(peak - 1, base - 1, -1): so the peak appears only once.
Using r spaces or rows - r misaligns the pyramid — rows lean or over-indent.
→ Use rows - 1 - r leading spaces so row 0 gets the most padding.
Magic numbers like chr(65 + r) work but break readability and lowercase variants.
→ Use base = ord('A') and chr(base + r) instead of raw ASCII values.
Non-numeric input raises ValueError with bare int(input()).
→ Wrap int(input()) in try/except ValueError and validate range.
Printing only palindrome letters without spaces produces a left-aligned pyramid like Program 18.
→ Print " " * (rows - 1 - r) before the ascend loop on every row.
Check these inputs before calling the solution done.
Output is just A (with rows - 1 spaces) — descend loop range is empty when peak equals base.
Treat as invalid; re-prompt instead of silent empty output.
26 rows with peak Z — bottom row prints ...Z...Z... with no leading spaces.
Clamp to 26 or define a wrap/error policy before printing.
Use try/except ValueError before clamping rows.
Same loops work with base = ord('a') and lowercase output.
Try these variations to lock in the pattern.
left and right stringsr prints 2r + 1 letters. Over n rows the total is n².for code in range(peak - 1, base - 1, -1): — the base - 1 stop is exclusive so A is included.left + right string variant is equivalent to the direct-print version — use whichever fits your lesson.Quick Takeaway: outer loop sets r, print (rows - 1 - r) spaces, ascend A..peak, descend (peak-1)..A, then break the line — that is the whole centered palindrome pyramid.
| Program | Time | Extra space |
|---|---|---|
| Ascend + descend loops (Examples 1–2) | O(rows²) | O(1) |
| String variant (Example 3) | O(rows²) | O(r) for left/right strings per row |
The centered alphabet palindrome pyramid combines centering spaces with mirror loops — ascend A..peak, descend (peak-1)..A. Master the direct-print version, then try the left/right string variant for clearer debugging.
Practice the three examples above, then continue to Program 33 in the alphabet pattern series.
Print leading spaces, run ascend and descend loops with peak - 1, clamp rows to 26, and compare with Program 18 (left-aligned palindrome pyramid).
base = ord('A'), clamp rows to 1–26(rows - 1 - r) leading spaces each rowrange(base, peak + 1), descend range(peak - 1, base - 1, -1)print(chr(code), end="") in letter loops; print() afterpeak — duplicates the center letterord('A')print() inside the letter loopsPrint the centered pyramid the beginner-friendly way.
A..peak..A
Definitionrows - 1 - r
Centerbase..peak
Codepeak-1..base
CodeO(n²) time
AnalysisRow 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 up: the widening alphabet triangle — build on symmetric shape ideas from this tutorial.
12 people found this page helpful