The string.replace()
is deprecated on python 3.x. What is the new way of doing this?
Python String | replace() replace() is an inbuilt function in the Python programming language that returns a copy of the string where all occurrences of a substring are replaced with another substring. Parameters : old – old substring you want to replace. new – new substring which would replace the old substring.
Python String replace() MethodThe replace() method replaces a specified phrase with another specified phrase. Note: All occurrences of the specified phrase will be replaced, if nothing else is specified.
When working with strings in Python, you may need to search through strings for a pattern, or even replace parts of strings with another substring. Python has the useful string methods find() and replace() that help us perform these string processing tasks.
As in 2.x, use str.replace()
.
Example:
>>> 'Hello world'.replace('world', 'Guido') 'Hello Guido'
replace()
is a method of <class 'str'>
in python3:
>>> 'hello, world'.replace(',', ':') 'hello: world'
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With