You are currently viewing How to replace a string with another in Python

In this article, we will take input from the user and replace the existing string with the one entered by user. This is yet another Python practice program for beginners. In the example code below, we already have a string (hardcoded) and then we will replace the already available string with the one entered by the user. We will use the replace function in this program.

s="hellopython" #hardcoded string that we will replace
newstring =input("enter a string: ")
#user entered the string heheonon
result=s.replace("hellopython",newstring)
print(result)

# Output: heheonon

Below is the output of the above code.

That’s it Folks on this topic. Let us know your comments on this article in the comment box below. Kindly like our facebook page, follows us on twitter and subscribe to our YouTube channel for latest updates.

Leave a Reply