0
0
PythonProgramBeginner · 2 min read

Python Program to Print Diamond Star Pattern

You can print a diamond star pattern in Python using nested loops like for i in range(n) to print spaces and stars in increasing and then decreasing order, for example: for i in range(n): print(' '*(n-i-1) + '*'*(2*i+1)) and then reverse for the bottom half.
📋

Examples

Input3
Output * *** ***** *** *
Input5
Output * *** ***** ******* ********* ******* ***** *** *
Input1
Output*
🧠

How to Think About It

To print a diamond star pattern, first print the top half with increasing stars centered by spaces, then print the bottom half with decreasing stars also centered. Use two loops: one for the top and one for the bottom, controlling spaces and stars carefully.
📐

Algorithm

1
Get input number n for diamond size
2
For each line i from 0 to n-1, print (n-i-1) spaces and (2*i+1) stars
3
For each line i from n-2 down to 0, print (n-i-1) spaces and (2*i+1) stars
4
End
💻

Code

python
n = int(input('Enter diamond size: '))
for i in range(n):
    print(' ' * (n - i - 1) + '*' * (2 * i + 1))
for i in range(n - 2, -1, -1):
    print(' ' * (n - i - 1) + '*' * (2 * i + 1))
Output
Enter diamond size: 3 * *** ***** *** *
🔍

Dry Run

Let's trace input n=3 through the code

1

Top half loop i=0

Print 2 spaces and 1 star: ' *'

2

Top half loop i=1

Print 1 space and 3 stars: ' ***'

3

Top half loop i=2

Print 0 spaces and 5 stars: '*****'

4

Bottom half loop i=1

Print 1 space and 3 stars: ' ***'

5

Bottom half loop i=0

Print 2 spaces and 1 star: ' *'

iSpacesStarsPrinted Line
021 *
113 ***
205*****
113 ***
021 *
💡

Why This Works

Step 1: Top half printing

The first loop prints the top half of the diamond by decreasing spaces and increasing stars using print(' ' * (n - i - 1) + '*' * (2 * i + 1)).

Step 2: Bottom half printing

The second loop prints the bottom half by increasing spaces and decreasing stars, reversing the top half pattern.

Step 3: Center alignment

Spaces before stars center the pattern horizontally, making the diamond shape symmetrical.

🔄

Alternative Approaches

Using string center method
python
n = int(input('Enter diamond size: '))
for i in range(n):
    print(('*' * (2 * i + 1)).center(2 * n - 1))
for i in range(n - 2, -1, -1):
    print(('*' * (2 * i + 1)).center(2 * n - 1))
This method uses Python's built-in <code>center()</code> to handle spaces, making code cleaner but slightly less manual.
Using a single loop with conditions
python
n = int(input('Enter diamond size: '))
for i in range(2 * n - 1):
    if i < n:
        stars = 2 * i + 1
        spaces = n - i - 1
    else:
        stars = 2 * (2 * n - i - 2) + 1
        spaces = i - n + 1
    print(' ' * spaces + '*' * stars)
This approach uses one loop for both halves, which can be more compact but slightly harder to read.

Complexity: O(n^2) time, O(1) space

Time Complexity

The program runs two loops each up to n, printing up to 2*n-1 characters per line, resulting in O(n^2) time.

Space Complexity

Only a few variables are used; printing is done directly, so space is O(1).

Which Approach is Fastest?

All approaches have similar time and space complexity; using center() is cleaner but not faster.

ApproachTimeSpaceBest For
Nested loops with manual spacesO(n^2)O(1)Clear control over spaces and stars
Using string center()O(n^2)O(1)Cleaner code, easier to read
Single loop with conditionsO(n^2)O(1)Compact code, slightly complex logic
💡
Use spaces to center stars so the diamond looks symmetrical.
⚠️
Beginners often forget to adjust spaces, causing the diamond to be left-aligned or uneven.