Skip to content
33 changes: 33 additions & 0 deletions graphics/butterfly_pattern.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
def butterfly_pattern(n: int) -> str:
"""
Creates a butterfly pattern of size n and returns it as a string.

>>> print(butterfly_pattern(3))
* *
** **
*****
** **
* *
"""
result = []

# Upper part
for i in range(1, n + 1):
left_stars = "*" * i
spaces = " " * (2 * (n - i))
right_stars = "*" * i
result.append(left_stars + spaces + right_stars)

# Lower part
for i in range(n - 1, 0, -1):
left_stars = "*" * i
spaces = " " * (2 * (n - i))
right_stars = "*" * i
result.append(left_stars + spaces + right_stars)

return "\n".join(result)


if __name__ == "__main__":
n = int(input("Enter the size of the butterfly pattern: "))
print(butterfly_pattern(n))
Loading