replace() is an inbuilt function in Python programming language that returns a copy of the string where all occurrences of a substring is replaced with another substring.
string.replace(old, new, count)
old – old substring you want to replace.
new – new substring which would replace the old substring.
count – the number of times you want to replace the old substring with the new substring. (Optional )
Return Value :
It returns a copy of the string where all occurrences of a substring is replaced with another substring.
- If count is not specified then all the occurences of old substring is replaced with the new substring.
- This method returns the copy of the string i.e. it does not changes the original string.
Below is the code demonstrating replace() :
Geeks for Geeks Geeks Geeks Geeks GeeksforGeeks for GeeksforGeeks GeeksforGeeks geeks geeks
gooks for gooks gooks gooks gooks geos for geos geos geeks geeks
Attention geek! Strengthen your foundations with the Python Programming Foundation Course and learn the basics.
To begin with, your interview preparations Enhance your Data Structures concepts with the Python DS Course. And to begin with your Machine Learning Journey, join the Machine Learning – Basic Level Course