0
0
PythonProgramBeginner · 2 min read

Python Program to Print Diamond Pattern

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

Examples

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

How to Think About It

To print a diamond pattern, first print the top half with increasing stars centered by spaces, then print the bottom half with decreasing stars also centered. Use loops to control the number of spaces and stars on each line.
📐

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 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: ' *'

iSpacesStarsLine Printed
021 *
113 ***
205*****
113 ***
021 *
💡

Why This Works

Step 1: Print top half

The first loop prints lines with increasing stars centered by spaces using print(' ' * (n - i - 1) + '*' * (2 * i + 1)).

Step 2: Print bottom half

The second loop prints lines with decreasing stars in reverse order to complete the diamond shape.

Step 3: Center alignment

Spaces before stars ensure the diamond is centered horizontally.

🔄

Alternative Approaches

Using while loops
python
n = int(input('Enter diamond size: '))
i = 0
while i < n:
    print(' ' * (n - i - 1) + '*' * (2 * i + 1))
    i += 1
i = n - 2
while i >= 0:
    print(' ' * (n - i - 1) + '*' * (2 * i + 1))
    i -= 1
Uses while loops instead of for loops; slightly longer but same logic.
Using string center method
python
n = int(input('Enter diamond size: '))
width = 2 * n - 1
for i in range(n):
    print(('*' * (2 * i + 1)).center(width))
for i in range(n - 2, -1, -1):
    print(('*' * (2 * i + 1)).center(width))
Uses string <code>center()</code> method for cleaner alignment.

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

Time Complexity

The program uses two loops each running about n times, and each line prints up to 2n-1 characters, so overall time is O(n^2).

Space Complexity

Only a few variables are used and output is printed directly, so space is O(1).

Which Approach is Fastest?

All approaches have similar time complexity; using center() may improve readability but not speed.

ApproachTimeSpaceBest For
For loops with spacesO(n^2)O(1)Simple and clear
While loopsO(n^2)O(1)Alternative loop style
String center methodO(n^2)O(1)Cleaner alignment
💡
Use spaces to center stars and print the top and bottom halves separately.
⚠️
Forgetting to reduce stars in the bottom half or misaligning spaces causes a broken diamond shape.