Python replace | How to replace word in python - codebugfree

Python replace word from string

In this section we learn the how to replace the word in python. In any case we change the word from string to specific variable than we use replace method for function.

Replace is a built function in python programming Language. Replace takes input the old word and takes the new word. That takes the old word and new word python replace old word to new word.

We see the code of python programming language language using the replace built in function. 

story = "always wite a code in morning when a sun is shine"
#string fuction
print("Now replacing the word....")
print(story.replace("code", "hanucharya"))

We make the variable as given string. And print the all string function and method like len, endswith, count, capitalize, find and last Replace.


Always wite a code in morning when a sun is shine
Now replacing the word....
always wite a hanucharya in morning when a sun is shine

Here the string different function and method work different task. len is calculate the total length. endswith return the boolen variable last endswith morning or not. Count is counting the specific alpabet that you give in input. Capitalize is make the capital first letter. Find is finding that word from starting to end and print that word character number. 

At last is replace is replacing the given old word to given new word.

user_pass_value = ("Visit website and use other website")
checking_pass = user_pass_value.replace("other website","code bug free")

Here  we change the word from string with the hwpl of replace. old word is other website and new word is code bug free.


Visit website and use code bug free

Post a Comment