Alphabet Diamond with * Separator in Python

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:
A
B*B
C*C*C
D*D*D*D
E*E*E*E*E
D*D*D*D
C*C*C
B*B
AComplete Python Program
Fixed rows = 5 version (cleaned from the reference; no ASCII constants):
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
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.
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.
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.
Diamond of starred repeats
The longest row has 2n − 1 tokens; overall length is on the order of n² — O(n²) output time, O(1) extra space aside from the joined string.
Variation — User Input Version
Read rows from input (clamped to 26):
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
65or69 - Duplicating the middle row twice (unless you want it repeated)
Key Takeaways
Top half increases counts from 1 to rows.
Bottom half decreases back to 1 without repeating the middle row.
* separators are easiest with join.
❓ Frequently Asked Questions
*-join a list like [ch] * count. This removes the need for a separate counter.* with any character (like - or #) or use sep.join([ch] * count).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.
10 people found this page helpful
