replace() function in python by R4R Team

- 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))



output-

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


-In this program, we operate replace() function with different strings and print replaced string too.




Leave a Comment:
Search
Categories
R4R Team
R4Rin Top Tutorials are Core Java,Hibernate ,Spring,Sturts.The content on R4R.in website is done by expert team not only with the help of books but along with the strong professional knowledge in all context like coding,designing, marketing,etc!