Python Program to Print Butterfly Star Pattern

Introduction

A butterfly star pattern consists of two parts: the upper part, where the stars expand outward, and the lower part, where the stars contract inward, resembling a butterfly's wings. This pattern is a great exercise to practice using nested loops and controlling output alignment.

Problem Statement

Create a Python program that:

  • Accepts the number of rows for the butterfly pattern.
  • Prints a butterfly-shaped star pattern.

Example:

  • Input: rows = 5
  • Output:
    *        *
    **      **
    ***    ***
    ****  ****
    **********
    ****  ****
    ***    ***
    **      **
    *        *
    

Solution Steps

  1. Input the Number of Rows: The user specifies the number of rows for the butterfly pattern.
  2. Use Nested Loops: The outer loops handle the rows, and the inner loops handle printing stars and spaces.
  3. Display the Butterfly Pattern: Print stars in increasing order for the upper part and decreasing order for the lower part, with spaces between the two sets of stars.

Python Program

# Step 1: Input the number of rows for the butterfly pattern
rows = int(input("Enter the number of rows: "))

# Step 2: Print the upper part of the butterfly
for i in range(1, rows + 1):
    # Print stars on the left side
    print("*" * i, end="")
    
    # Print spaces between the two wings
    print(" " * (2 * (rows - i)), end="")
    
    # Print stars on the right side
    print("*" * i)

# Step 3: Print the lower part of the butterfly
for i in range(rows, 0, -1):
    # Print stars on the left side
    print("*" * i, end="")
    
    # Print spaces between the two wings
    print(" " * (2 * (rows - i)), end="")
    
    # Print stars on the right side
    print("*" * i)

Explanation

Step 1: Input the Number of Rows

  • The program starts by asking the user for the number of rows, which defines the height of the butterfly's wings.

Step 2: Print the Upper Part of the Butterfly

  • The outer loop controls the rows for the upper part of the butterfly.
    • The first part prints stars on the left side.
    • The second part prints spaces in the middle to separate the two sides of the butterfly's wings.
    • The third part prints stars on the right side.

Step 3: Print the Lower Part of the Butterfly

  • The second loop controls the rows for the lower part, which is a mirror image of the upper part.
    • The logic is the same as the upper part but in reverse order.

Output Example

For rows = 5, the output will be:

*        *
**      **
***    ***
****  ****
**********
****  ****
***    ***
**      **
*        *

For rows = 4, the output will be:

*      *
**    **
***  ***
********
***  ***
**    **
*      *

Conclusion

This Python program prints a butterfly pattern using nested loops to create symmetrical wings. The stars are printed with spaces in between to form the butterfly shape, and the pattern is mirrored to complete the butterfly design. This exercise helps in practicing loop control, alignment, and output formatting in Python.

Comments