Hollow Diamond Inside Square Star Pattern in Python

What You'll Learn
You print a frame with a solid row of * on the first line and again on the last line, and mirror the rows in between so the middle row is narrowest (only the left and right border stars with a wide hollow gap).
The width is 2 * rows; the number of lines is 2 * rows - 1. This layout differs from the standalone hollow diamond outline (Program 9).
⭐ Pattern Output
When you run the program with rows = 5:
**********
**** ****
*** ***
** **
* *
** **
*** ***
**** ****
**********Complete Python Program
Fixed rows = 5 version:
rows = 5
height = 2 * rows - 1
width = 2 * rows
for line in range(1, height + 1):
if line == 1 or line == height:
print("*" * width)
else:
i = line if line <= rows else (2 * rows - line)
left = rows - i + 1
gap = 2 * (i - 1)
print(("*" * left) + (" " * gap) + ("*" * left))🧠 How It Works
Grid size
height = 2 * rows - 1 lines; width = 2 * rows characters per line (even width for the closed frame). For rows = 4 that is 7 lines × 8 columns, matching the variation sample.
Top and bottom bars
for line in range(1, height + 1): — when line == 1 or line == height, print("*" * width) draws a full solid edge.
Inner rows: left, gap, right
Else branch: i = line if line <= rows else (2 * rows - line), left = rows - i + 1, gap = 2 * (i - 1). One print(("*" * left) + (" " * gap) + ("*" * left)) builds the hollow middle; the gap widens to the waist then mirrors.
Finish each line
Each branch ends with a single print that outputs exactly width characters (then the default newline). No extra trailing spaces after the right star block.
Diamond in a frame
O(n²) output for n = rows (each of 2n-1 lines writes up to 2n cells), O(1) extra space. For rows = 5 the bars are 10 characters wide—the green preview scrolls horizontally on narrow screens.
Variation — User Input Version
Read rows from user input:
rows = int(input("Enter the number of rows: "))
height = 2 * rows - 1
width = 2 * rows
for line in range(1, height + 1):
if line == 1 or line == height:
print("*" * width)
else:
i = line if line <= rows else (2 * rows - line)
left = rows - i + 1
gap = 2 * (i - 1)
print(("*" * left) + (" " * gap) + ("*" * left))💡 Tips for Enhancement
Try These
- Validate
rows > 0before printing - Print only the hollow diamond outline (Program 9) for comparison
- Change the frame character (e.g., use
#) - Build each line as a string and print once per row
- Try making the frame hollow (border only) instead of solid top/bottom bars
Avoid
- Using
2 * rows - 1as width (this pattern uses2 * rows) - Forgetting to mirror
ifor the bottom half - Mixing tabs and spaces (alignment breaks)
- Forgetting newline after each line
- Assuming user input is always valid
Key Takeaways
Width is 2 * rows and height is 2 * rows - 1.
Top and bottom lines are fully filled with stars (frame).
Middle lines are symmetric using mirrored i values.
Time complexity is O(n²) for n rows.
This is a different layout than the standalone hollow diamond (Program 9).
❓ Frequently Asked Questions
2 * rows and height 2 * rows - 1 so the top and bottom can be solid horizontal bars while the inner rows close on the left and right sides.2 * rows - 1. Program 11 adds a frame-like look using width 2 * rows and solid top/bottom rows.Explore: All Star Patterns
Browse the full Python star pattern series.
This pattern is a popular variation because it combines a frame-like look with a symmetric hollow diamond.
9 people found this page helpful
