PyDocs

Nav

Python Runtime Services

str.replace

builtins

method of str
  • str.replace(old, new, /, count=-1)
Return a copy with all occurrences of substring old replaced by new.

  count
    Maximum number of occurrences to replace.
    -1 (the default value) means replace all occurrences.

If count is given, only the first count occurrences are replaced.
If count is not specified or -1, then all occurrences are replaced.
Changed in 3.13
  • 3.13: count is now supported as a keyword argument.
1 Example
>>> 'spam, spam, spam'.replace('spam', 'eggs')
'eggs, eggs, eggs'
>>> 'spam, spam, spam'.replace('spam', 'eggs', 1)
'eggs, spam, spam'
from the Python docs
See Also
No see-alsos for builtins/str.replace
0 Notes
No notes for str.replace