replace()

A string method that returns a copy of a string with every occurrence, or the first few, of one piece of text replaced by another.

In more detail

s.replace("a", "o") changes all; s.replace("a", "o", 1) only the first. The original string is unchanged, because strings are immutable, so assign the result: s = s.replace(...).

Example

"banana".replace("a", "o", 2)   # "bonona"

Related terms

Taught in

References