Alphabet Diamond in Python

What You’ll Learn
This program prints the same widening letter rows as Program 33 (apex A, then B B, …), then mirrors them downward so the shape closes back to A. The widest row appears only once.
⭐ Pattern Output
When you run the program with rows = 5 (half height from apex to the widest row):
A
B B
C C
D D
E E
D D
C C
B B
AComplete Python Program
Fixed rows = 5 (no magic ASCII values). Top half, then bottom half:
rows = 5
rows = max(1, min(rows, 26))
base = ord('A')
# Top half: r = 0 .. rows-1
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()
# Bottom half: mirror without repeating the widest row
for r in range(rows - 2, -1, -1):
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
One row, one rule
For row index r, print rows - 1 - r spaces, then chr(base + r). If r > 0, print 2 * r - 1 spaces and the same letter again.
Grow: top half
for r in range(rows) prints from the tip through the widest row (for rows = 5, A through E).
Shrink: bottom half
for r in range(rows - 2, -1, -1) reuses the same body. Starting at rows - 2 skips the widest row so it is not printed twice.
Closed diamond
Total printed rows are 2n − 1 for half-height n. Work per row is O(n), so total time is O(n²).
Variation — User Input Version
Read half height from input (clamped to 26):
rows = int(input("Enter number of rows (half height, 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()
for r in range(rows - 2, -1, -1):
print(" " * (rows - 1 - r), end="")
ch = chr(base + r)
print(ch, end="")
if r > 0:
print(" " * (2 * r - 1), end="")
print(ch, end="")
print()💡 Tips for Enhancement
Try These
- Use
ord('a')for a lowercase diamond - Extract the row body into a function to avoid duplicating the loop
- After this series, try number patterns
Avoid
- Starting the bottom loop at
rows - 1(duplicates the widest row) - Hardcoding ASCII values instead of
ord('A')
Key Takeaways
Top: r in 0 .. rows-1.
Bottom: r in rows-2 .. 0.
Same spacing and gap rules as Program 33.
❓ Frequently Asked Questions
rows - 2 and counts down to 0.r to mirror the shape.n, so O(n²) overall.Next: Python Number Pattern Programs
Continue with number pattern tutorials in Python.
10 people found this page helpful
