What's Happening
A diamond pattern splits into two mirror halves: an upper pyramid and a lower inverted pyramid. Each row needs careful spacing before and after the stars. Hollow versions show just the outline, while filled versions fill the middle. We'll focus on the hollow 7-row pattern here—it's a common interview question and homework assignment.
Step-by-Step Solution
We'll use Python 3.12 (current as of 2026). Open your terminal and follow these steps.
- Fire up your favorite Python environment—IDLE, VS Code, or Jupyter will all work.
- Create a new file called
diamond.pyand paste this code:rows = 7 mid = rows // 2 for i in range(mid + 1): spaces = ' ' * (mid - i) stars = '*' * (2 * i + 1) print(spaces + stars) for i in range(mid - 1, -1, -1): spaces = ' ' * (mid - i) stars = '*' * (2 * i + 1) print(spaces + stars) - Save the file (Ctrl + S).
- Run it (F5 in IDLE or
python diamond.pyin terminal). - Watch the hollow diamond appear in your console. Each line shows only the outline.
| Line | Code Output (7 rows) |
|---|---|
| 1 | * |
| 2 | *** |
| 3 | ***** |
| 4 | ******* |
| 5 | ***** |
| 6 | *** |
| 7 | * |
If This Didn't Work
- Wrong number of rows? Change the
rowsvariable. Odd numbers (3, 5, 7…) keep the symmetry intact. Even numbers? They mess up the shape. - 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? Make sure you're printing just two stars per line (start and end), except for the top and bottom rows.
Prevention Tips
- Start small. Try 3 rows first. Once that works, move up to 5, then 7.
- Use comments. Add
# Upper halfand# Lower halfabove each loop to stay organized. - Print variables. Temporarily print
spacesandstarsseparately to double-check your counts. - Bookmark a reference: Python Control Flow Docs.
How do the loops create the diamond?
Each loop handles half the pattern. The first loop runs from the top row down to the middle, adding more stars and fewer spaces each time. The second loop runs from just below the middle back up to the top, reversing the process. That's how you get the mirror effect.
Can I make a filled diamond instead?
Change the star calculation to print stars from the first space to the last. For a 7-row filled diamond, replace the star line with stars = '*' * (2 * mid + 1) in both loops. Honestly, this is the simplest version to code.
Why use odd numbers for rows?
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?
rows variable to any odd number.Try 5 rows, 9 rows, even 11. The pattern scales automatically. Just make sure to keep it odd—your diamond will thank you.
How do I center the diamond in the console?
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 for most cases.
Can I print this to a file instead?
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?
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?
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?
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?
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?
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?
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.
