Hollow Number Diamond Pattern in Python

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:
1
2 2
3 3
4 4
5 5
4 4
3 3
2 2
1Complete Python Program
We print the top half (1..n) and then mirror the same logic for the bottom half (n-1..1).
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
Top half rows (1..n)
The first outer loop runs from 1 to n and prints the expanding top half.
Left edge positioning
The first inner loop prints the left edge number at the correct indentation by checking i == j.
Right edge positioning
The second inner loop prints the same row number again on the right edge by checking i == k.
Mirror the bottom half
The second outer loop runs from n-1 down to 1 and repeats the same logic to mirror the diamond.
Hollow diamond
We print only the edges and leave the interior as spaces.
Variation — User Input Version
Read n from the user and print the same hollow diamond.
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
Top half prints rows 1..n; bottom half prints n-1..1.
Only edge positions print numbers; the inside is spaces.
Row 1 prints once because edges overlap.
Runtime is roughly O(n²) for n rows.
❓ Frequently Asked Questions
i iterating from n-1 down to 1.1 at the bottom.Explore More Python Number Patterns!
Diamond patterns are a great next step after mastering triangles and hollow shapes.
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.
7 people found this page helpful
