Filled Diamond Star Pattern in Python

Beginner
⏱️ 9 min read
📚 Updated: Aug 2025
🎯 2 Code Examples
2n − 1 rows total

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:

Output
    *    
   ***   
  *****  
 ******* 
*********
 ******* 
  *****  
   ***   
    *    
1

Complete Python Program

Fixed rows = 5 version:

Python
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

1

Upper half (i = 1rows)

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).

Pyramid
2

Lower half (i = rows - 11)

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.

Mirror
3

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.

Odd counts
4

New line every row

Each print(...) call ends with a newline automatically. Both outer loops rely on that one statement per iteration.

Line break
=

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.

2

Variation — User Input Version

Read rows from user input:

Python
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

1

A filled diamond uses two halves: increasing odd stars then decreasing odd stars.

2

Each row prints rows - i spaces and 2 * i - 1 stars.

3

Total output lines are 2 * rows - 1.

4

Time complexity is O(n²) for n rows.

5

This is the filled version of the hollow diamond (Program 9).

❓ Frequently Asked Questions

Odd star counts keep the diamond centered around one middle column. Each next row changes star count by 2, expanding or shrinking evenly on both sides.
The top half already prints the widest row when i == rows. Starting at rows - 1 prevents printing the middle row twice.
It’s O(n²) because there are Theta(n) lines and each line prints Theta(n) characters.

Next: Diamond in a Frame

Continue to Program 11 to print a diamond inside a square-style frame.

Program 11 →
Did you know?

A filled diamond is basically a pyramid followed by an inverted pyramid, with the middle row printed only once.

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.

9 people found this page helpful