DEV Community

Codes With Pankaj
Codes With Pankaj

Posted on

Python code to create a butterfly pattern using asterisks

# Butterfly Pattern in Python
# Website: @codeswithpankaj

def butterfly_pattern(n):
    for i in range(n):
        for j in range(i + 1):
            print("*", end=" ")
        spaces = 2 * (n - i - 1)
        for j in range(spaces):
            print(" ", end=" ")
        for j in range(i + 1):
            print("*", end=" ")
        print()
    for i in range(n - 1, 0, -1):
        for j in range(i):
            print("*", end=" ")
        spaces = 2 * (n - i)
        for j in range(spaces):
            print(" ", end=" ")
        for j in range(i):
            print("*", end=" ")
        print()
# Example usage with n=5
butterfly_pattern(5)
Output
* * * * *         * * * * *
* * * *             * * * *
* * *                 * * *
* *                     * *
*                         *
*                         *
* *                     * *
* * *                 * * *
* * * *             * * * *
* * * * *         * * * * *
Enter fullscreen mode Exit fullscreen mode

Image of Datadog

The Essential Toolkit for Front-end Developers

Take a user-centric approach to front-end monitoring that evolves alongside increasingly complex frameworks and single-page applications.

Get The Kit

Top comments (1)

Collapse
 
jocomvag profile image
Jocom Vag

Billboard image

The Next Generation Developer Platform

Coherence is the first Platform-as-a-Service you can control. Unlike "black-box" platforms that are opinionated about the infra you can deploy, Coherence is powered by CNC, the open-source IaC framework, which offers limitless customization.

Learn more

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay