Python Inverted V Alphabet Pattern

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

What Is This Pattern?

A widening alphabet triangle starts with a centered apex A, then prints the same letter twice on each lower row with a growing gap between them.

Remember
Rule: (rows-1-r) pads + letter
      if r > 0: gap (2*r-1) + same letter

    A
   B B
  C   C
 D     D
E       E     ← 5 rows

Compare Program 32 (full palindrome rows like ABCBA). Program 34 mirrors this triangle upward into a diamond.

How to Solve It

For each row: pad, print the letter, then for r > 0 print a gap and the same letter again.

MethodIdeaBest for
Staged printPads, letter, optional gap + mirrorLearning, interviews, exams
One-line rowBuild pad + ch + (gap + ch) as a stringDebugging; inspect the full row

Pseudocode

Pseudocode
for r from 0 to rows - 1:
    print (rows - 1 - r) spaces
    ch = 'A' + r
    print ch
    if r > 0:
        print (2 * r - 1) spaces
        print ch
    print newline

Cheat sheet

GoalPattern
Walk rowsfor r in range(rows):
Leading padsprint(" " * (rows - 1 - r), end="")
Row letterch = chr(ord("A") + r)
Gap size2 * r - 1 (only when r > 0)
Mirror letterprint(" " * (2 * r - 1) + ch, end="")
ApexSkip gap when r == 0
End the rowprint()
vs Program 32Same letter twice + gap — not A..peak..A

Printing Letters vs Starting a New Line

APIEffectUse for
print(..., end="")Stays on the same rowPads, letter, gap, mirror letter
print()Ends the current rowAfter the whole row is built

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

Live Preview

Change the row count and the widening triangle updates instantly — including gap size on the bottom row.

Whole numbers from 1 to 10. Size 5 means bottom letter E with a 7-space gap. Tap a chip or type a value — the preview redraws as you go.

Live result 5 rows · bottom E
    A
   B B
  C   C
 D     D
E       E

Worked Walkthrough — rows = 3

Pads shrink while the gap between mirrored letters grows.

rPads / gapPrinted row
02 pads + A (no gap)A
11 pad + B + 1 space + BB B
20 pads + C + 3 spaces + CC C

Row r prints O(r) gap spaces; summing over n rows is O(n²).

Python Programs

Three complete programs: fixed height, row-count input(), and a one-line row builder. Use View Output for sample results.

Example 1 — Fixed rows = 5

Pad, print the letter, then optionally print a gap and the same letter again.

Python
rows = 5
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()

How It Works

1. Center. " " * (rows - 1 - r) pads the left so the triangle stays centered.

2. Letter. ch = chr(base + r) is A, B, C, D, E on successive rows.

3. Gap + mirror. When r > 0, print 2*r - 1 spaces and the same letter again. Row 0 skips this block so the apex is a single A.

Example 2 — Row Count Input

Read how many rows to print. Cap at 26 so letters 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="")
    ch = chr(base + r)
    print(ch, end="")
    if r > 0:
        print(" " * (2 * r - 1), end="")
        print(ch, end="")
    print()

How It Works

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

2. Same stages. Three rows produce A, B B, and C C with 2, 1, and 0 leading spaces.

Example 3 — One-Line Row Builder

Combine pad, letter, and conditional gap into one string, then print once.

Python
rows = 5
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)

How It Works

1. Build then print. pad and ch are fixed; the ternary adds gap + mirror only when r > 0.

2. Inspect easily. Identical shape to Example 1, but you can check row before printing.

Edge Cases & Pitfalls

Check these before calling the solution done.

double A

Gap block on row 0

Without if r > 0, row 0 tries a negative or empty gap and may print a second A. Guard the mirror.

wrong gap

2 * r instead of 2 * r - 1

Even gaps misalign the triangle. Classic samples use odd gaps: 1, 3, 5, 7…

no pads

Missing leading spaces

Without (rows - 1 - r) pads the shape leans left instead of staying centered.

newline early

Bare print() mid-row

Use end="" for pads and letters; call bare print() only after the full row.

rows = 1

Single A

No pads and no gap — just A. A good sanity check.

Bad input

Non-integer rows

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

Time and Space Complexity

ProgramTimeExtra space
Staged print (Examples 1–2)O(n²)O(1)
Row builder (Example 3)O(n²)O(n) per row for the string

Row r prints O(r) gap spaces (plus pads). Summing over n rows is quadratic.

Key Takeaways

  • Rule: pads + letter; if r > 0, gap 2*r-1 + same letter.
  • Apex: row 0 prints a single centered A — skip the mirror.
  • Contrast: Program 32 fills the middle with letters; this pattern leaves a hollow gap.
  • Next step: Program 34 mirrors this triangle into a full alphabet diamond.

One line: for each row, print pads + letter, then (if not the apex) gap 2*r-1 + the same letter.

Frequently Asked Questions

Each row prints the same letter twice with a growing gap between them, except row 0 which prints a single centered A. Row r uses (rows-1-r) leading spaces, letter chr(ord('A')+r), then for r>0 a gap of 2*r-1 spaces and the same letter again.
Row 0 is the apex — only one A should appear at the top. The if r > 0 guard prevents printing a gap and duplicate letter on the first row.
Print (rows - 1 - r) leading spaces before the first letter. Row 0 gets the most spaces; the bottom row gets zero.
For r > 0, print (2*r - 1) spaces between the two copies of the row letter. Row 1 gets 1 gap space (B B), row 2 gets 3 (C C), row 4 gets 7 (E E).
Program 32 prints palindrome rows (A, ABA, ABCBA) with ascending and descending letter loops. Program 33 prints the same letter twice per row with a widening space gap — not a full palindrome string.
Each row letter is chr(ord('A') + r). With rows > 26 you would need characters beyond Z unless you define a wrap or error policy.
O(n²) where n is rows. Row r prints O(r) gap spaces plus O(n) leading spaces; the sum over all rows is O(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 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: Alphabet Diamond

Widen from A to E with this hollow row rule, then narrow back to A for a full diamond.

Program 34 tutorial →

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