- replace() is a python build-in function which are used to replace the content of the string with other content.
Syntax-
replace(old,new,number)
- Here old is that content which we want to replace
- and new is that content which we want to put at the place of old.
- number shows that how many time you want to replace in one string, by default it replace in whole string.
Example
string is s="Home page"
replace("Home","House") will give "House page"
string is s="is is is is is"
replace("is","will",2) will give "will will is is is"
program-
s="this is the string is is is is"
print(s.replace("is","was"))
s="this is the string is is is is"
print(s.replace("is","was",3))
s="my text is awesome"
print(s.replace("is",""))
s="this is the string is is is is"
print(s.replace(" ","-"))
print("Enter string")
s=input()
print("Enter characters to replace")
s1=input()
print("Enter character to put at the place of replace")
s2=input()
print("Your string is ",s)
print("string afte replace",s.replace(s1,s2))
thwas was the string was was was was
thwas was the string was is is is
my text awesome
this-is-the-string-is-is-is-is
Enter string
I am good programmer
Enter characters to replace
good
Enter character to put at the place of replace
bad
Your string is I am good programmer
string afte replace I am bad programmer