Python Program to Print Butterfly Pattern



This python program prints butterfly pattern made of stars up to n number of lines given by user.


In this program, we first read even number of rows from user. We divide entire butterfly pattern into two parts, lower part and upper part and then we generate individual pattern one by one.

Python Source Code: Generate Butterfly Pattern


row = int(input("Enter number of rows (even): "))

n = row//2

print("Generated butterfly pattern is:\n")
# Upper part
for i in range(1,n+1):
    for j in range(1, 2*n+1):
        if j>i and j< 2*n+1-i:
            print("  ", end="")
        else:
            print("* ", end="")
    print()

# Lower part
for i in range(n,0,-1):
    for j in range(2*n,0,-1):
        if j>i and j< 2*n+1-i:
            print("  ", end="")
        else:
            print("* ", end="")
    print()   

Output

Enter number of rows (even): 12
Generated butterfly pattern is:

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