0
0
PythonProgramBeginner · 2 min read

Python Program to Print Butterfly Pattern

You can print a butterfly pattern in Python using nested loops like 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 pattern, think of it as two mirrored triangles of stars separated by spaces. The top half increases stars on both sides while decreasing spaces in the middle. The bottom half reverses this pattern. Use loops to print stars and spaces accordingly for each line.
📐

Algorithm

1
Get input number n for the size of the butterfly.
2
For each line from 1 to n, print stars equal to the line number, then spaces equal to twice (n - line), then stars again equal to the line number.
3
For each line from n down to 1, repeat the same pattern 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 to see how the butterfly pattern forms.

1

Top half, line 1

Print 1 star, 4 spaces, 1 star: '* *'

2

Top half, line 2

Print 2 stars, 2 spaces, 2 stars: '** **'

3

Top half, line 3

Print 3 stars, 0 spaces, 3 stars: '******'

4

Bottom half, line 3

Print 3 stars, 0 spaces, 3 stars: '******'

5

Bottom half, line 2

Print 2 stars, 2 spaces, 2 stars: '** **'

6

Bottom half, line 1

Print 1 star, 4 spaces, 1 star: '* *'

LineStars LeftSpacesStars RightOutput
1141* *
2222** **
3303******
3303******
2222** **
1141* *
💡

Why This Works

Step 1: Stars on both sides

Each line prints stars on the left and right sides using * i where i is the current line number.

Step 2: Spaces in the middle

Spaces between stars decrease as the line number increases, calculated by 2 * (n - i) to keep symmetry.

Step 3: Top and bottom halves

The top half builds up stars and reduces spaces, while the bottom half reverses this to complete the butterfly shape.

🔄

Alternative Approaches

Using string formatting with join
python
n = 5
for i in range(1, n + 1):
    print(''.join(['*' for _ in range(i)]) + ' ' * (2 * (n - i)) + ''.join(['*' for _ in range(i)]))
for i in range(n, 0, -1):
    print(''.join(['*' for _ in range(i)]) + ' ' * (2 * (n - i)) + ''.join(['*' for _ in range(i)]))
This uses list comprehension and join to build star strings, which is more flexible but slightly longer.
Using a function to print one line
python
def print_line(i, n):
    print('*' * i + ' ' * (2 * (n - i)) + '*' * i)
n = 5
for i in range(1, n + 1):
    print_line(i, n)
for i in range(n, 0, -1):
    print_line(i, n)
This approach improves readability by separating line printing into a function.

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

Time Complexity

The program uses two loops each running n times, and each line prints up to 2*n characters, resulting in O(n^2) time.

Space Complexity

The program uses constant extra space for variables and prints directly, so space complexity is O(1).

Which Approach is Fastest?

All approaches have similar time complexity; using string multiplication is fastest and simplest for this pattern.

ApproachTimeSpaceBest For
String multiplicationO(n^2)O(1)Simplicity and speed
List comprehension with joinO(n^2)O(1)Flexibility in building strings
Function abstractionO(n^2)O(1)Readability and reuse
💡
Use nested loops carefully to control stars and spaces for symmetrical patterns.
⚠️
Beginners often forget to double the spaces in the middle, breaking the butterfly shape symmetry.