0
0
PythonProgramBeginner · 2 min read

Python Program to Print Butterfly Star Pattern

You can print a butterfly star pattern in Python using nested for loops that print stars and spaces in a mirrored shape; for example, use for i in range(1, n+1): print('*' * i + ' ' * (2*(n-i)) + '*' * i) for the top half and a similar loop for the bottom half.
📋

Examples

Inputn = 3
Output* * ** ** ****** ****** ** ** * *
Inputn = 5
Output* * ** ** *** *** **** **** ********** ********** **** **** *** *** ** ** * *
Inputn = 1
Output** **
🧠

How to Think About It

To print a butterfly star pattern, think of it as two mirrored triangles of stars separated by spaces. The top half increases stars on each side while decreasing spaces in the middle, and the bottom half reverses this. Use loops to print stars and spaces accordingly for each line.
📐

Algorithm

1
Get input number n for the size of the pattern
2
For each line from 1 to n, print stars equal to the line number, then spaces equal to twice the difference between n and the line, then stars again equal to the line number
3
For each line from n down to 1, repeat the same printing logic to form the bottom half
4
Print each line to form the butterfly shape
💻

Code

python
n = 5
for i in range(1, n + 1):
    print('*' * i + ' ' * (2 * (n - i)) + '*' * i)
for i in range(n, 0, -1):
    print('*' * i + ' ' * (2 * (n - i)) + '*' * i)
Output
* * ** ** *** *** **** **** ********** ********** **** **** *** *** ** ** * *
🔍

Dry Run

Let's trace n=3 through the code

1

Top half, i=1

Print '*' * 1 + ' ' * 4 + '*' * 1 => '* *'

2

Top half, i=2

Print '*' * 2 + ' ' * 2 + '*' * 2 => '** **'

3

Top half, i=3

Print '*' * 3 + ' ' * 0 + '*' * 3 => '******'

4

Bottom half, i=3

Print '*' * 3 + ' ' * 0 + '*' * 3 => '******'

5

Bottom half, i=2

Print '*' * 2 + ' ' * 2 + '*' * 2 => '** **'

6

Bottom half, i=1

Print '*' * 1 + ' ' * 4 + '*' * 1 => '* *'

iStars LeftSpacesStars RightLine Output
1* ** *
2** **** **
3************
3************
2** **** **
1* ** *
💡

Why This Works

Step 1: Stars on left and right

We print stars on both sides equal to the current line number using '*' * i to create the wings of the butterfly.

Step 2: Spaces in the middle

The spaces between stars are twice the difference between total lines and current line, 2 * (n - i), to keep the shape symmetric.

Step 3: Top and bottom halves

The top half increases stars and decreases spaces, while the bottom half reverses this to complete the butterfly shape.

🔄

Alternative Approaches

Using single loop with conditional
python
n = 5
for i in range(1, 2*n + 1):
    if i <= n:
        stars = i
    else:
        stars = 2*n - i + 1
    spaces = 2*(n - stars)
    print('*' * stars + ' ' * spaces + '*' * stars)
This uses one loop for both halves, simplifying code but adding a condition inside the loop.
Using functions for halves
python
def print_half(n, start, end, step):
    for i in range(start, end, step):
        print('*' * i + ' ' * (2 * (n - i)) + '*' * i)

n = 5
print_half(n, 1, n+1, 1)
print_half(n, n, 0, -1)
This approach uses a function to avoid repeating code, improving readability.

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

Time Complexity

The program uses two loops each running up to n times, so the time complexity is O(n^2).

Space Complexity

No extra space proportional to input size is used; only variables for counters, so space complexity is O(1).

Which Approach is Fastest?

All approaches have similar time complexity; using a single loop with condition may be slightly faster but less readable.

ApproachTimeSpaceBest For
Two separate loopsO(n^2)O(1)Clear structure, easy to understand
Single loop with conditionO(n^2)O(1)Compact code, slight speed gain
Function for halvesO(n^2)O(1)Reusable and clean code
💡
Use nested loops carefully to control stars and spaces for symmetrical patterns.
⚠️
Beginners often forget to double the spaces in the middle, breaking the symmetry.