Widening Rule
letter + gap + letter
Each row prints one letter, then for r > 0 a gap of 2*r - 1 spaces and the same letter again.

Each row mirrors the same letter with a growing gap: row 0 prints centered A, row 1 prints B B, row 2 prints C C, until row 4 shows E E for five rows. Leading spaces (rows - 1 - r) center the triangle; the gap formula 2*r - 1 widens each row. Compare with Program 32 (centered palindrome pyramid) and Program 19 (mirrored halves). Includes a live preview, worked Python examples, edge cases, and complexity.
letter + gap + letter
Each row prints one letter, then for r > 0 a gap of 2*r - 1 spaces and the same letter again.
rows - 1 - r
print(" " * (rows - 1 - r), end="") centers each row under the single apex A.
chr(base + r)
ch = chr(base + r) picks the row letter — A on row 0, B on row 1, and so on.
2*r - 1
print(" " * (2 * r - 1), end="") widens the gap between mirrored letters each row.
1–26 rows
Pick a row count and draw the widening alphabet triangle in the browser instantly.
Complexity
Row r prints 2*r - 1 gap spaces when r > 0; total output ≈ O(n²); extra memory stays O(1).
A widening alphabet triangle prints each row as the same letter twice with a growing space gap, padded with leading spaces so the shape is centered. Row 0 prints a single A; each next row steps to the next letter — B B, C C, D D, and so on.
In Python you solve it with an outer loop over row index r, leading spaces, one letter, an optional gap guarded by if r > 0, and a mirrored copy of the same letter — or build each row as a single string for clarity.
It combines three classic pattern skills — centering with spaces, conditional row logic, and a widening gap formula — the same building blocks used in hollow pyramids, diamonds, and symmetric ASCII art. Compare with Program 32 to see palindrome rows vs mirrored same-letter pairs.
" " * (rows - 1 - r) — row 0 gets rows - 1 spaces; bottom row gets none.
ch = chr(base + r) — prints the row letter once before the gap.
if r > 0: then gap spaces and the same ch again — skipped on row 0.
if r > 0 ensures row 0 prints only one A, not A A.
In short: set base = ord('A'), loop r from 0 to rows - 1, print (rows - 1 - r) spaces, print ch, if r > 0 print (2*r - 1) gap spaces and ch again, then print() for the newline.
Given a positive integer rows, print a centered triangle of rows lines. Row r prints (rows - 1 - r) leading spaces, then letter chr(ord('A') + r). For r > 0, print (2*r - 1) gap spaces and the same letter again.
# First 5 rows (widening triangle)
A
B B
C C
D D
E E | Item | Type | Description |
|---|---|---|
rows | int | Number of rows (row letter runs A through the rows-th letter). Clamp to 1–26 for A–Z demos. |
| Printed output | text | Widening alphabet triangle: each row mirrors the same letter with a growing gap — bottom row has 2*rows - 1 spaces between the two letters plus leading spaces. |
base = ord('A')
for r from 0 to rows-1:
print (rows-1-r) leading spaces
ch = chr(base + r)
print ch
if r > 0:
print (2*r - 1) gap spaces
print ch
print newline | Approach | Idea | Best for |
|---|---|---|
| Direct print with gap guard | Leading spaces, one letter, if r > 0 gap + mirror letter | Learning conditional row logic and gap formula |
| One-line row builder | pad + ch + ((gap + ch) if r > 0 else "") string expression | Clearer debugging and row inspection |
| Program 32 contrast | See Program 32 (palindrome rows) | Palindrome rows vs same-letter mirror pairs |
| Goal | Pattern |
|---|---|
| Leading spaces | print(" " * (rows - 1 - r), end="") |
| Outer loop (row index) | for r in range(rows): |
| Row letter | ch = chr(base + r) |
| Gap spaces (r > 0) | print(" " * (2 * r - 1), end="") |
| Mirror letter (r > 0) | print(ch, end="") inside if r > 0: |
| End the row | print() |
| Row builder variant | pad + ch + ((" " * (2*r-1) + ch) if r > 0 else "") |
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.
2*r - 1
1, 3, 5, 7...Widens the space between mirrored letters — row 1 gets 1, row 4 gets 7.
same ch
if r > 0Prints the same letter again after the gap — guarded by if r > 0 so row 0 stays a single A.
mirrored halves
different gapMirrored pattern with spaces between halves — see Program 19.
Reach for widening alphabet triangles when teaching conditional row logic, gap formulas, and symmetric same-letter pairs after palindrome pyramids.
Program 32 prints palindrome rows (A, ABA, ABCBA). This pattern mirrors the same letter with a widening gap instead.
Master the 2*r - 1 gap formula before tackling full diamonds and hollow shapes.
The (rows - 1 - r) space formula appears in centered stars, numbers, and diamond patterns.
Next pattern closes into a full alphabet diamond — another symmetric shape variation.
This is a console teaching pattern — not how you build modern app screens.
Key benefit: one program that combines centering spaces with a widening gap formula — 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 widening alphabet triangle in the browser.
Three complete Python programs — fixed five rows with gap/mirror 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 widening alphabet triangle 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):
print(" " * (rows - 1 - r), end="")
ch = chr(base + r)
print(ch, end="")
if r > 0:
print(" " * (2 * r - 1), end="")
print(ch, end="")
print() The outer loop walks row index r from 0 to 4. For each row, print(" " * (rows - 1 - r), end="") centers the row, then ch = chr(base + r) picks the row letter. For r > 0, print(" " * (2 * r - 1), end="") widens the gap before the mirrored letter. Row 0 prints only A with four leading spaces; row 4 prints E E with a 7-space gap and no leading 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="")
ch = chr(base + r)
print(ch, end="")
if r > 0:
print(" " * (2 * r - 1), end="")
print(ch, end="")
print() Same widening triangle core as Example 1; only the row count comes from input. Three rows produce A, B B, and C C with 2, 1, and 0 leading spaces respectively.
Build each row as a single string expression with pad, letter, and optional gap.
Combine pad, letter, and conditional gap in one row string — same logic, easier row inspection.
rows = 5
rows = max(1, min(rows, 26))
base = ord('A')
for r in range(rows):
pad = " " * (rows - 1 - r)
ch = chr(base + r)
row = pad + ch + ((" " * (2 * r - 1) + ch) if r > 0 else "")
print(row) pad holds leading spaces and the ternary adds gap + mirror letter only when r > 0. Building row as one string produces identical output to Examples 1 and 2, but you can inspect each row before printing 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 chr(base + r), then mirror (letter-1)..A with print(chr(code), end="") in both loops.
print() ends the row after leading spaces, letter, optional gap, and mirror finish; the outer loop advances r.
Total characters grow with gap spaces: row r prints O(r) gap spaces — O(n²) time, O(1) extra memory (loop version).
rows = 5Trace each outer-loop value of r and see how leading spaces, row letter, gap, and mirror letter produce each widening row.
r | letter | lead spaces | gap | mirror | full row |
|---|---|---|---|---|---|
| 0 | A | 4 | (none) | (none) | A |
| 1 | B | 3 | 1 | B | B B |
| 2 | C | 2 | 3 | C | C C |
| 3 | D | 1 | 5 | D | D D |
| 4 | E | 0 | 7 | E | E E |
Highlight rows: r = 0 (4 lead spaces, A only), r = 1 (3 spaces, B + 1 gap + B → B B), r = 4 (0 spaces, E + 7 gap + E). Gap grows by 2 each row: 2*r - 1 gives 1, 3, 5, 7 for rows 1–4.
Where widening alphabet triangles show up beyond the homework prompt.
Program 32 prints palindrome rows (A, ABA, ABCBA). This pattern mirrors the same letter with a widening gap instead.
Example: compare palindrome ABCBA rows vs B B / C C mirror pairs side by side.
Reinforce the 2*r - 1 gap formula before tackling hollow pyramids and full diamonds.
Example: trace row 2 (r=2) on paper: lead spaces=2, letter=C, gap=3, mirror=C.
Mirrored halves with spaces between — see Program 19.
Example: compare Program 19’s mirrored halves with this same-letter widening gap approach.
Mirror the triangle downward to close a full alphabet diamond.
Example: after the top half, loop r from rows-2 down to 0 with the same gap + mirror row logic.
Sum of widening gap spaces makes O(n²) concrete for beginners.
Example: 5 rows → gap spaces 0+1+3+5+7 = 16 plus 9 letters.
Classic nested-loop question that tests gap formula and centering spaces.
Example: explain why row 0 skips the gap and mirror without running code.
Pro Tip: say “leading spaces, letter, if r>0 gap then same letter” before coding — that story prevents double A on row 0 and wrong gap width.
Why this pattern earns a spot after the centered palindrome pyramid from Program 32.
if r > 0 gap guard — a pattern reused whenever row 0 is special.
Leading spaces create a visually balanced pyramid — every row aligns under the apex.
Direct print loops for learning; one-line row builder for clearer debugging.
Streaming output needs no storage beyond loop counters (row builder uses O(r) per row string).
Pro Tip: when row 0 prints only A, the mirror loop range is empty — that is correct, not a bug.
Small habits that keep widening alphabet triangle code clean.
Use ch = chr(base + r) — keeps gap and mirror 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.
if r > 0: must wrap gap and mirror — never print a second A on row 0.
Trace A, B B, C C with 2, 1, 0 leading spaces on paper before coding larger demos.
Pro Tip: if gaps look too narrow, check the formula — it should be 2*r - 1, not 2*r.
Mistakes that commonly break widening alphabet triangles.
Forgetting if r > 0 prints A A on row 0 — two letters at the apex.
→ Wrap gap and mirror in if r > 0: so row 0 prints only one A.
Using 2*r instead of 2*r - 1 makes gaps one space too wide starting at row 1.
→ Use 2*r - 1 for the gap — row 1 needs 1 space, row 4 needs 7.
Using r lead spaces or rows - r misaligns the triangle — rows lean or over-indent.
→ Use rows - 1 - r leading spaces so row 0 gets the most padding.
Non-numeric input raises ValueError with bare int(input()).
→ Wrap int(input()) in try/except ValueError and validate range.
Printing gap + mirror on every row including r=0 produces A A instead of a single centered A.
→ Keep gap and mirror inside if r > 0: on every row.
Check these inputs before calling the solution done.
Output is just A with no leading spaces when rows=1 — gap block skipped because r=0.
Treat as invalid; re-prompt instead of silent empty output.
26 rows with letter 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.
r prints 2r - 1 gap spaces when r > 0. Over n rows the total gap spaces sum to (n-1)².if r > 0: — without it row 0 would print A A instead of a single apex.Quick Takeaway: outer loop sets r, print (rows - 1 - r) spaces, print ch, if r > 0 print gap and ch again, then break the line — that is the whole widening alphabet triangle.
| Program | Time | Extra space |
|---|---|---|
| Direct print with gap guard (Examples 1–2) | O(rows²) | O(1) |
| Row builder variant (Example 3) | O(rows²) | O(r) for row string per row |
The widening alphabet triangle combines centering spaces with a growing gap and mirrored same-letter rows. Master the direct-print version, then try the one-line row builder for clearer debugging.
Practice the three examples above, then continue to Program 34 in the alphabet pattern series.
Print leading spaces, guard row 0 with if r > 0, use gap formula 2*r - 1, clamp rows to 26, and compare with Program 32 (centered palindrome pyramid).
base = ord('A'), clamp rows to 1–26(rows - 1 - r) leading spaces each rowch, then if r > 0: gap 2*r-1 and mirror chprint(ch, end="") for letters; print() after each roword('A')2*r for gap — gaps one space too wideif r > 0 — row 0 prints two lettersPrint the widening triangle the beginner-friendly way.
letter + gap + letter
Definitionrows - 1 - r
Center2*r - 1
Codeif r > 0
CodeO(n²) time
AnalysisRow r prints (rows - 1 - r) leading spaces, then letter chr(ord('A') + r). For r > 0, a gap of 2*r - 1 spaces separates a second copy of the same letter. Row 0 is a single centered A.
Next up: the alphabet diamond — extend this widening triangle into a full symmetric shape.
12 people found this page helpful