Alphabet Diamond with * Separator in Python

Beginner
⏱️ 6 min read
📚 Updated: Aug 2025
🎯 2 Code Examples
up + down

What You'll Learn

This pattern prints a top half from A to E and a bottom half back to A, repeating the same letter with * between them.

⭐ Pattern Output

When you run the program with rows = 5:

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

Complete Python Program

Fixed rows = 5 version (cleaned from the reference; no ASCII constants):

Python
rows = 5
rows = max(1, min(rows, 26))

base = ord('A')

def print_row(letter_code: int, count: int) -> None:
    ch = chr(letter_code)
    print("*".join([ch] * count))

# Top half: A..(A+rows-1)
for i in range(rows):
    print_row(base + i, i + 1)

# Bottom half: (A+rows-2)..A
for i in range(rows - 2, -1, -1):
    print_row(base + i, i + 1)

🧠 How It Works

1

Helper: one row of ch with stars

print_row(letter_code, count) builds a list of count copies of the same character and joins them with *, e.g. C*C*C.

Helper
2

Top half: widen to the peak

for i in range(rows) calls print_row(base + i, i + 1), so letter and repeat count both grow until the middle row.

Up
3

Bottom half: mirror without repeating peak

for i in range(rows - 2, -1, -1) mirrors the top using the same counts, stepping letters back toward A.

Down
=

Diamond of starred repeats

The longest row has 2n − 1 tokens; overall length is on the order of O(n²) output time, O(1) extra space aside from the joined string.

2

Variation — User Input Version

Read rows from input (clamped to 26):

Python
rows = int(input("Enter the number of rows (max 26): "))
rows = max(1, min(rows, 26))

base = ord('A')

for i in range(rows):
    ch = chr(base + i)
    print("*".join([ch] * (i + 1)))

for i in range(rows - 2, -1, -1):
    ch = chr(base + i)
    print("*".join([ch] * (i + 1)))

💡 Tips for Enhancement

Try These

  • Change separator: use "-".join(...) instead of "*".join(...)
  • Print lowercase by using ord('a') as the base
  • Center each row with spaces for a true diamond look

Avoid

  • Hardcoding ASCII values like 65 or 69
  • Duplicating the middle row twice (unless you want it repeated)

Key Takeaways

1

Top half increases counts from 1 to rows.

2

Bottom half decreases back to 1 without repeating the middle row.

3

* separators are easiest with join.

❓ Frequently Asked Questions

Build each row as a string: *-join a list like [ch] * count. This removes the need for a separate counter.
Yes. Replace * with any character (like - or #) or use sep.join([ch] * count).
It’s O(n²) for n rows, because total printed characters grow with the number of rows.

Next: Python Alphabet Pattern 22

Continue to Program 22 for the next alphabet pattern in Python.

Program 22 →

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