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(len(story))
print(story.endswith("morning"))
print(story.count("a"))
print(story.capitalize())
print(story.find("code"))
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.
INOUTPUT
49
False
4
Always wite a code in morning when a sun is shine
14
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")
print(checking_pass)
Here we change the word from string with the hwpl of replace. old word is other website and new word is code bug free.
INOUTPUT
Visit website and use code bug free
0 Comments
Don't comment a spam message