Hollow Number Diamond Pattern in Python

Beginner
⏱️ 7 min read
📚 Updated: Aug 2025
🎯 2 Code Examples
Mirror Rows

What You’ll Learn

How to print a hollow number diamond in Python. The top half grows from 1 to n, and the bottom half mirrors back down to 1.

This is a neat practice problem for building symmetry with two outer loops and conditional printing.

⭐ Pattern Output

For n = 5, the pattern looks like this:

Output
     1
    2 2
   3   3
  4     4
 5       5
  4     4
   3   3
    2 2
     1
1

Complete Python Program

We print the top half (1..n) and then mirror the same logic for the bottom half (n-1..1).

Python
n = 5

for i in range(1, n + 1):
    for j in range(n, 0, -1):
        if i == j:
            print(j, end="")
        else:
            print("", end=" ")
    for k in range(2, n + 1):
        if i == k:
            print(k, end="")
        else:
            print("", end=" ")
    print()

for i in range(n - 1, 0, -1):
    for j in range(n, 0, -1):
        if i == j:
            print(j, end="")
        else:
            print("", end=" ")
    for k in range(2, n):
        if i == k:
            print(k, end="")
        else:
            print("", end=" ")
    print()

🧠 How It Works

1

Top half rows (1..n)

The first outer loop runs from 1 to n and prints the expanding top half.

Top
2

Left edge positioning

The first inner loop prints the left edge number at the correct indentation by checking i == j.

Left edge
3

Right edge positioning

The second inner loop prints the same row number again on the right edge by checking i == k.

Right edge
4

Mirror the bottom half

The second outer loop runs from n-1 down to 1 and repeats the same logic to mirror the diamond.

Bottom
=

Hollow diamond

We print only the edges and leave the interior as spaces.

2

Variation — User Input Version

Read n from the user and print the same hollow diamond.

Python
n = int(input("Enter n: "))
if n < 1:
    raise ValueError("n must be at least 1")

for i in range(1, n + 1):
    for j in range(n, 0, -1):
        print(j if i == j else " ", end=" ")
    for k in range(2, n + 1):
        print(k if i == k else " ", end=" ")
    print()

for i in range(n - 1, 0, -1):
    for j in range(n, 0, -1):
        print(j if i == j else " ", end=" ")
    for k in range(2, n):
        print(k if i == k else " ", end=" ")
    print()

💡 Tips for Enhancement

Try These

  • Print * instead of numbers for a hollow star diamond
  • Use fixed-width cells if you print multi-digit values
  • Convert it to a filled diamond by printing numbers for all inner positions
  • Build each row as a list and join it for cleaner formatting
  • Try printing a centered pyramid version instead of this diagonal/edge version

Avoid

  • Forgetting the bottom half (the diamond won’t be complete)
  • Mixing 0-based and 1-based indices in conditions
  • Printing without spacing (output becomes hard to see)
  • Skipping input validation for n

Key Takeaways

1

Top half prints rows 1..n; bottom half prints n-1..1.

2

Only edge positions print numbers; the inside is spaces.

3

Row 1 prints once because edges overlap.

4

Runtime is roughly O(n²) for n rows.

❓ Frequently Asked Questions

Yes. The bottom half is the same logic with i iterating from n-1 down to 1.
It keeps the printed width consistent with the mirrored top half so the last row prints a single 1 at the bottom.
Yes. You still get a symmetric diamond; the spacing just changes slightly based on the width.
O(n²), since the amount of printing (including spaces) scales quadratically.

Explore More Python Number Patterns!

Diamond patterns are a great next step after mastering triangles and hollow shapes.

All Number Patterns →
Did you know?

Many hollow patterns can be solved by printing a value only when a cell is on a boundary (edges or diagonals) and printing spaces otherwise.

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.

7 people found this page helpful