Returns a copy of a string with one piece of text swapped for another.
Synopsis
text.replace(old, new)
Description
replace changes every place old appears. It is case-sensitive.
Parameters
| Name | Meaning |
|---|---|
old |
The text to find. |
new |
The text to put in its place. |
Return value
A new str.
Example
print("I like cats".replace("cats", "dogs"))
print("a b c".replace(" ", ""))
print("banana".replace("a", "o"))
It prints:
I like dogs
abc
bonono
Common mistake
print("Hello".replace("h", "J"))
It prints:
Hello
Nothing changed, because "h" and "H" are different characters.
See also
Last updated 2 weeks ago