Chapters
Python114 chapters

ReferenceString methods

str.replace()

Swaps every occurrence of one piece of text for another.

Signature

text.replace(old, new, count=-1)

Parameters

NameMeans
oldwhat to look for
newwhat to put there
counthow many to replace at most

Returns

A new string.

Example

Python
print("one,two".replace(",", " | "))
print("aaa".replace("a", "b", 2))

Output

one | two
bba

See also