Quick Fix: Set rows=7 in the script below, run it in any Python 3.12+ console, and you’ll get a hollow diamond in one click.
What's Happening
A diamond pattern is two mirrored triangles.
Think of it this way: the top half grows wider with each line, while the bottom half shrinks back down. We’re printing a hollow outline here—the kind you’d see in a coding interview. (Filled diamonds are easier, but we’ll get to those later.)
Step-by-Step Solution
Open a Python 3.12+ console and run this code.
Tested on Python 3.12 as of 2026. Grab any editor that runs Python—VS Code, IDLE, even a terminal will work.
- Create a new file called
diamond.py and paste this in:
rows = 7
mid = rows // 2
# Upper half
for i in range(mid + 1):
print(' ' * (mid - i) + '*' + ' ' * (2 * i - 1) + ('*' if i > 0 else ''))
# Lower half
for i in range(mid - 1, -1, -1):
print(' ' * (mid - i) + '*' + ' ' * (2 * i - 1) + ('*' if i > 0 else ''))
- Save it (Ctrl + S in most editors).
- Run the file:
- In IDLE? Hit F5.
- Using VS Code? Click the Run button or press Ctrl+F5.
- Terminal users? Type
python3 diamond.py.
- Here’s what you should see:
| Line | Console Output |
| 1 | * |
| 2 | * * |
| 3 | * * |
| 4 | * * |
| 5 | * * |
| 6 | * * |
| 7 | * |
If This Didn’t Work
Check these common issues to fix your diamond pattern.
- Shape looks wrong? Double-check that
rows is an odd number (3, 5, 7…). Even numbers break the symmetry completely.
- Stars are stuck together? The padding logic matters. Make sure
' ' * (2 * i - 1) is in every line except the very top and bottom.
- Blank line at the top? Scan your code for any extra
print() statements before the loops start.
Prevention Tips
- Start small. Test with 3 rows first. Once it looks right, bump it to 5, then 7.
- Count your prints. Temporarily add
print("spaces:", mid-i, "stars:", 2*i+1) inside the loop to verify your math.
- Label your loops. Add
# top half and # bottom half above each section so you don’t get lost.
- Bookmark the Python control-flow docs if you need a refresher on loop syntax.
If This Didn't Work
Change the rows variable to an odd number.
- Wrong number of rows? Odd numbers (3, 5, 7…) keep the symmetry intact. Even numbers? They’ll wreck your diamond.
- Stars overlapping? Look at your loop ranges. The first loop runs from 0 to mid inclusive; the second runs from mid-1 down to 0.
- Hollow version not hollow? Each line should only have two stars (start and end), except for the top and bottom rows.
How do the loops create the diamond?
The first loop builds the top half, the second loop builds the bottom half.
Each loop handles half the pattern. The first loop starts at the top and works its way down to the middle, adding more stars and fewer spaces each time. The second loop then reverses the process, shrinking back up to the top. That mirroring effect is what gives you the diamond shape.
Can I make a filled diamond instead?
Yes—just print stars across the entire width of each row.
For a filled diamond, replace the star line with stars = '*' * (2 * mid + 1) in both loops. Honestly, this is the simplest version to code—no fancy spacing tricks needed.
Why use odd numbers for rows?
Odd numbers keep the diamond perfectly symmetrical.
With odd rows, you get a clear middle row and equal spacing on both sides. Even rows? They create an awkward split where the middle "row" isn’t centered. Most tutorials use odd numbers for this exact reason.
What if I want a different size?
Change the rows variable to any odd number.
Try 5 rows, 9 rows, even 11. The pattern scales automatically. Just remember to keep it odd—your diamond will thank you.
How do I center the diamond in the console?
Add extra spaces to the left of each line.
Console width varies, but most terminals handle 80 characters well. For a 7-row diamond, add about 10 extra spaces to each line. You can calculate it dynamically with console_width - len(line), but a fixed number works fine in most cases.
Can I print this to a file instead?
Yes—redirect the output to a text file.
Run your script with python diamond.py > diamond.txt in the terminal. The diamond will save to a file instead of printing to the screen. Open it in any text editor to see your handiwork.
What about other programming languages?
Same logic applies—just adjust the syntax.
JavaScript, C++, even Bash can print diamond patterns. The loops and spacing rules stay the same; you just swap out the print statements and loop syntax. That said, Python makes this one of the cleanest implementations.
How do I make a hollow diamond with a different character?
Replace the asterisk with any character you like.
Swap '*' for '#', '@', or even emoji like '💎'. The spacing logic stays identical. Just make sure your terminal supports the character you choose.
Can I animate the diamond drawing?
Yes—use a delay between each line.
Add import time at the top and time.sleep(0.2) after each print. The diamond will draw itself line by line. For extra flair, clear the console between frames with os.system('cls' if os.name == 'nt' else 'clear').
What's the fastest way to debug this?
Print the spaces and stars separately before combining them.
Temporarily print print(spaces) and print(stars) on separate lines. This shows exactly how many spaces and stars each row contains. Once you verify the counts, combine them back into a single print statement.
Are there libraries that can help?
Not really—this is simple enough to code from scratch.
Libraries like numpy or matplotlib would be overkill for this task. The nested loops approach is straightforward and teaches core programming concepts. That said, if you're printing complex patterns regularly, a dedicated library might save time down the road.
How do I share my diamond pattern?
Copy the output and paste it anywhere.
Select the diamond in your terminal, copy it, and paste it into a chat, email, or document. The pattern maintains its shape as long as the font is monospaced. Most code editors and terminals use monospace fonts by default, so you're usually good to go.
Edited and fact-checked by the TechFactsHub editorial team.