Alphabet Diamond in Python

Beginner
⏱️ 7 min read
📚 Updated: Aug 2025
🎯 2 Code Examples
diamond, mirror

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):

Output
    A
   B B
  C   C
 D     D
E       E
 D     D
  C   C
   B B
    A
1

Complete Python Program

Fixed rows = 5 (no magic ASCII values). Top half, then bottom half:

\n Python
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

1

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.

Row
2

Grow: top half

for r in range(rows) prints from the tip through the widest row (for rows = 5, A through E).

Phase 1
3

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.

Phase 2
=

Closed diamond

Total printed rows are 2n − 1 for half-height n. Work per row is O(n), so total time is O(n²).

2

Variation — User Input Version

Read half height from input (clamped to 26):

\n Python
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

1

Top: r in 0 .. rows-1.

2

Bottom: r in rows-2 .. 0.

3

Same spacing and gap rules as Program 33.

❓ Frequently Asked Questions

The first loop already printed the widest row. The second loop should continue with the next smaller rows, so it starts at rows - 2 and counts down to 0.
Yes. Program 33 is only the top half; this program runs the same line logic again for decreasing r to mirror the shape.
About 2n − 1 rows with O(n) characters each for half-height n, so O(n²) overall.

Next: Python Number Pattern Programs

Continue with number pattern tutorials in Python.

Number Patterns →

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.

10 people found this page helpful