Hollow Diamond Star Pattern in Python

What You'll Learn
This pattern is Program 7 (inverted V, upper half) plus the lower half using the same outer idea as Program 8: after i runs 1…rows, run i from rows - 1 down to 1 with the same hollow-gap logic.
Each printed line has width 2 * rows - 1 (9 characters when rows = 5).
⭐ Pattern Output
When you run the program with rows = 5:
*
* *
* *
* *
* *
* *
* *
* *
*Complete Python Program
Fixed rows = 5 version:
rows = 5
# Upper half (same idea as Program 7)
for i in range(1, rows + 1):
print(" " * (rows - i), end="")
print("*", end="")
if i > 1:
print(" " * (2 * i - 3), end="")
print("*", end="")
print()
# Lower half (same idea as Program 8), start from rows - 1 to avoid duplicate middle row
for i in range(rows - 1, 0, -1):
print(" " * (rows - i), end="")
print("*", end="")
if i > 1:
print(" " * (2 * i - 3), end="")
print("*", end="")
print()🧠 How It Works
Upper half (i = 1 … rows)
for i in range(1, rows + 1): repeats Program 7’s body: margin string, first *, optional " " * (2 * i - 3) and second *, then print(). The waist row (i == rows) is the widest.
Lower half (i = rows - 1 … 1)
for i in range(rows - 1, 0, -1): runs the same row logic as the upper half so the shape narrows again. Starting at rows - 1 skips duplicating the widest row.
Per-row pieces
Every row uses print(" " * (rows - i), end=""), print("*", end=""), and when i > 1 the gap plus second star—all without newlines until the final print() on that row.
Width
Each printed line has 2 * rows - 1 characters. Total lines: rows + (rows - 1) = 2 * rows - 1.
Full hollow diamond
O(n²) output for n = rows, O(1) extra space. Width 2n - 1 scrolls horizontally in the green preview on narrow screens.
Variation — User Input Version
Read rows from user input:
rows = int(input("Enter the number of rows: "))
for i in range(1, rows + 1):
print(" " * (rows - i), end="")
print("*", end="")
if i > 1:
print(" " * (2 * i - 3), end="")
print("*", end="")
print()
for i in range(rows - 1, 0, -1):
print(" " * (rows - i), end="")
print("*", end="")
if i > 1:
print(" " * (2 * i - 3), end="")
print("*", end="")
print()💡 Tips for Enhancement
Try These
- Print a filled diamond by using Program 5 + Program 6 logic (Program 10)
- Change the character from
*to#or@ - Use string-building to create each line and print once per row
- Validate
rows > 0before printing - Try adding a frame around the diamond (Program 11)
Avoid
- Starting the lower half at
rows(middle row duplicates) - Mixing tabs and spaces (alignment breaks)
- Forgetting newline between rows
- Printing the second star when
i == 1 - Assuming user input is always valid
Key Takeaways
The diamond is built by printing the upper half then mirroring it as the lower half.
Each row is 2 * rows - 1 characters wide.
Lower half starts at rows - 1 to avoid duplicating the middle row.
Time complexity is O(n²) for n rows.
This diamond uses the same edge logic as Programs 7 and 8.
❓ Frequently Asked Questions
rows - 1 prevents printing the middle row twice.Next: Filled Diamond Pattern
Continue to Program 10 to print a filled diamond star pattern in Python.
The hollow diamond is a classic pattern exercise because it combines symmetry, loops, and careful boundary printing.
9 people found this page helpful
