Python program to print the heart shape pattern with your name.



 

To understand this first of all we have to understand the pattern of the heart shape. For this, we divide the pattern into three parts and their explanation is given in the image below:


So after understanding the code, now it is easy to understand where we have to put our name so for this, we must add a message before starting the 3rd part of our code.

And the code for this will be:

print("* "*((n//2)+1) + "".join(msg) + " *"*((n//2)+1))

 

So finally, the python code will be:

num=int(input("Enter the number: "))
n=num//2
msg=input("Enter your name: ")
l=len(msg)

for i in range(n):
    print(" "*(n-i-1) + "* "*(i+1),end="")
    if num%2==0:
        for j in range(2*(n-i-1)):
            print(" ",end="")
    else:
        for j in range(2*(n-i-1)+1):
            print(" ",end="")
    for j in range(i+1):
        print("* ", end="")
    print()
print("* "*((n//2)+1) + "".join(msg) + " *"*((n//2)+1))

for i in range(num,0,-1):
    print(" " * (num - i) + "* " * i )

The output will be:



I hope you like this article

Thanks


32 views0 comments

Recent Posts

See All