A right-aligned alphabet pyramid prints A..i on each row (letters restart from A), with leading spaces so every row shares the same right edge.
Remember
Rule: pad left; each row restarts A..i (width-2 cells)
A
A B
A B C
A B C D
A B C D E ← top = E
Contrast Program 22 (continuous counter across rows) and Program 16 (centered pads). Here each row is a fresh A..i prefix, right-aligned.
Approach
How to Solve It
For each row letter, print leading spaces, then print A through that letter in width-2 fields.
Method
Idea
Best for
Letter-code loops
Outer i = row letter; pad top..i+1; letters A..i
Learning the classic ord/chr style
Int row index
Pad n - row; then row letters via ord('A') + L
Thinking in row numbers
Pseudocode
Pseudocode
top = code of 'E' // or chosen top
for i from 'A' to top: // row letter
for j from top down while j > i:
print space (no newline)
for k from 'A' to i:
print k in width-2 field (no newline)
print newline
Cheat sheet
Goal
Pattern
Walk each row letter
for i in range(ord("A"), ord("E") + 1):
Leading pads
for j in range(top, i, -1): print(" ", end="")
Letters A..i
for k in range(ord("A"), i + 1): print(f"{chr(k):>2}", end="")
Pad count
top - i (or n - row in int style)
End the row
print()
vs Program 22
Restart from A each row — no running counter
Printing Letters vs Starting a New Line
API
Effect
Use for
print(" ", end="") / print(f"{ch:>2}", end="")
Stays on the same row
Each pad and each letter cell
print()
Ends the current row
After pad + letter loops
Print without a newline, then end the row once.
Try it
Live Preview
Change the size (top letter) and the right-aligned pyramid updates instantly — including pad count on the first row.
Whole numbers from 1 to 10. Size 5 means top letter E. Tap a chip or type a value — the preview redraws as you go.
Live result5 letters · top E · width 5
A
A B
A B C
A B C D
A B C D E
Trace
Worked Walkthrough — top = D
Trace pads and the A..i prefix for each row letter (width-2 letter cells).
Row letter
Pads
Letters
Printed row
A
3
A
A
B
2
A B
A B
C
1
A B C
A B C
D
0
A B C D
A B C D
Each of n rows does O(n) pad + letter work → O(n²) total.
Code
Python Programs
Three complete programs: fixed A–E, top-letter input(), and an int row-index style. Use View Output for sample results.
Example 1 — Fixed A–E
First print leading spaces, then print letters A..i using f"{chr(k):>2}" formatting.
Python
for i in range(ord("A"), ord("E") + 1):
for j in range(ord("E"), i, -1):
print(" ", end="")
for k in range(ord("A"), i + 1):
print(f"{chr(k):>2}", end="")
print()
Output
A
A B
A B C
A B C D
A B C D E
How It Works
1. Outer loop picks the row letter.i runs from A to E.
2. Pad first.range(ord("E"), i, -1) prints one space per step above the current letter.
3. Restart A..i. Each letter uses f"{chr(k):>2}" so columns stay even in monospace fonts.
When i is C: two leading pads, then A B C. Pads shrink until the bottom row needs none.
Example 2 — Top Letter Input
The pattern prints up to that row. Use input().strip().upper() and validate a single A–Z character.
Python
raw = input("Enter top letter (like E): ").strip().upper()
if len(raw) != 1 or not ("A" <= raw <= "Z"):
print("Please enter a single letter A-Z.")
raise SystemExit(1)
top = ord(raw)
for i in range(ord("A"), top + 1):
for j in range(top, i, -1):
print(" ", end="")
for k in range(ord("A"), i + 1):
print(f"{chr(k):>2}", end="")
print()
Output (when user enters C)
A
A B
A B C
How It Works
1. Prompt and validate. Strip, uppercase, and require a single A–Z letter.
2. Same pad + prefix core. Only the shared top bound changes — pad count is always top - i.
Example 3 — Int Row Index
Often clearer if you think in row numbers: pad n - row spaces, then print row letters from A.
Python
top = ord("E")
n = top - ord("A") + 1
for row in range(1, n + 1):
for s in range(n - row):
print(" ", end="")
for L in range(row):
print(f"{chr(ord('A') + L):>2}", end="")
print()
Output
A
A B
A B C
A B C D
A B C D E
How It Works
1. Pad by row number. Pad count is n - row.
2. Letters from A. Letter L is chr(ord('A') + L) — row 1 prints A, row 3 prints A B C.
Same visual pyramid as the letter-code scan — only the indexing style changes.
Edge Cases & Pitfalls
Check these before calling the solution done.
running counter
Continuous letters across rows
If you keep a k++ stream, you wrote Program 22. This pattern always restarts from A.
width mismatch
Bare letters without :>2
Without width-2 fields, spacing looks uneven next to the leading pads. Keep f"{chr(k):>2}".
newline early
Bare print() inside loops
Use end="" for pads and letters; call bare print() only after both inner loops.
vs Program 16
Right vs centered
Program 16 uses more padding for a centered look. This pattern only right-aligns.
top = A
Single A
Output is just a width-2 A cell (no pads) — a good sanity check.
Bad input
Validate one letter
Reject empty strings and multi-character input before computing top.
Analysis
Time and Space Complexity
Program
Time
Extra space
Letter-code loops (Examples 1–2)
O(n²)
O(1) beyond the printed characters
Int row index (Example 3)
O(n²)
O(1) beyond the printed characters
Each of n rows prints O(n) pads plus O(n) letter cells, so total work is quadratic in the number of letters.
Remember
Key Takeaways
Rule: pad left; each row restarts A..i with width-2 cells.
Pads:top - i spaces shrink each lower row.
Break the row: use end="" for cells; bare print() after both loops.
Complexity:O(n²) from pad + letter work per row.
One line: for each row letter i, print top - i spaces, then A..i in width-2 fields.
Frequently Asked Questions
The first inner loop prints (top - i) leading spaces, fewer on each lower row, so the letter block ends at the same right edge.
Width-2 formatting prints each letter in an even column so the output matches the spaced layout (best viewed in a monospace font).
Program 22 uses a continuous counter across all rows. Program 27 restarts letters from A on every row and uses leading spaces to push rows to the right.
Remove the leading-space loop and print letters directly from A to the row letter.
O(n²) for n rows because each row prints O(n) spaces plus O(n) letters.
Read a line, strip it, call .upper(), require a single A–Z character, and reject empty or multi-character input.
Program 16 centers the pyramid with more padding. This pattern only right-aligns by shrinking leading spaces while restarting A..i each row.
Yes. Pad n − row spaces, then print row letters as chr(ord('A') + L). Example 3 on this page shows that style.
🤔
Did you know?
Leading padding: for each row letter i, the loop prints top - i spaces. Then the letter loop prints A through i using f"{chr(k):>2}" so columns look even in monospace output. The last row has no padding; all rows share the same right edge.