Filled Diamond Star Pattern in Python

What You'll Learn
This program prints a filled diamond using two halves: the upper half increases stars by 2 each row, and the lower half decreases stars by 2, keeping the pattern centered with leading spaces.
For a given row index i, the star count is 2 * i - 1. Total output lines are 2 * rows - 1.
⭐ Pattern Output
When you run the program with rows = 5:
*
***
*****
*******
*********
*******
*****
***
* Complete Python Program
Fixed rows = 5 version:
rows = 5
for i in range(1, rows + 1):
print(" " * (rows - i) + "*" * (2 * i - 1))
for i in range(rows - 1, 0, -1):
print(" " * (rows - i) + "*" * (2 * i - 1))🧠 How It Works
Upper half (i = 1 … rows)
for i in range(1, rows + 1): prints one line per row: print(" " * (rows - i) + "*" * (2 * i - 1)). Star counts are 1, 3, 5, …, 2*rows-1; string repetition builds the whole line in a single print (adds a newline by default).
Lower half (i = rows - 1 … 1)
for i in range(rows - 1, 0, -1): uses the same expression. As i shrinks, the margin grows and the star run shortens—for rows = 5 the lower segments have 7, 5, 3, 1 stars.
Why 2 * i - 1?
Odd widths keep one center star per row. Each line has (rows - i) + (2i - 1) = rows + i - 1 characters before the newline.
New line every row
Each print(...) call ends with a newline automatically. Both outer loops rely on that one statement per iteration.
Solid diamond
2 * rows - 1 lines total; widest line has 2 * rows - 1 stars. O(n²) characters for n = rows, O(1) extra space. The green preview scrolls sideways on phones when the middle row is wider than the viewport.
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) + "*" * (2 * i - 1))
for i in range(rows - 1, 0, -1):
print(" " * (rows - i) + "*" * (2 * i - 1))💡 Tips for Enhancement
Try These
- Print a hollow diamond by printing stars only on the edges (Program 9)
- Use a different character (like
#) - Validate input before printing
- Experiment with printing spaces between stars
- Try the framed diamond variation next (Program 11)
Avoid
- Duplicating the middle row (start the lower half from
rows - 1) - Mixing tabs and spaces for alignment
- Forgetting newline after each row
- Using even star counts (symmetry breaks)
- Assuming user input is always valid
Key Takeaways
A filled diamond uses two halves: increasing odd stars then decreasing odd stars.
Each row prints rows - i spaces and 2 * i - 1 stars.
Total output lines are 2 * rows - 1.
Time complexity is O(n²) for n rows.
This is the filled version of the hollow diamond (Program 9).
❓ Frequently Asked Questions
i == rows. Starting at rows - 1 prevents printing the middle row twice.Next: Diamond in a Frame
Continue to Program 11 to print a diamond inside a square-style frame.
A filled diamond is basically a pyramid followed by an inverted pyramid, with the middle row printed only once.
9 people found this page helpful
