Skip to main content

How Do You Print A Diamond Pattern?

by
Last updated on 6 min read

What's Happening

You're building a symmetric shape from stars and spaces that looks like a diamond.

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

Use two loops to print the upper and lower halves of the diamond.

We'll use Python 3.12 (current as of 2026). Open your terminal and follow these steps.

  1. Fire up your favorite Python environment—IDLE, VS Code, or Jupyter will all work.
  2. Create a new file called diamond.py and 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)
  3. Save the file (Ctrl + S).
  4. Run it (F5 in IDLE or python diamond.py in terminal).
  5. 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

Check these common issues to fix your diamond pattern.
  • Wrong number of rows? Change the rows variable. 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 and verify each step to avoid frustration.
  • Start small. Try 3 rows first. Once that works, move up to 5, then 7.
  • Use comments. Add # Upper half and # Lower half above each loop to stay organized.
  • Print variables. Temporarily print spaces and stars separately to double-check your counts.
  • Bookmark a reference: Python Control Flow Docs.

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 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?

Yes—just print stars across the entire width of each row.

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?

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 make sure 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 for 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.

Sarah Kim
Author

Sarah Kim is a home repair specialist and certified home inspector who's been fixing things since she helped her dad rewire the family garage at 14. She writes practical DIY guides and isn't afraid to tell you when a job needs a licensed professional.

What Is The Best Portable Satellite Dish For Directv?What Is Lnbf Stand For?