You are here : python_2stringstringreplace

string.replace() - string


The method replace() returns a copy of the string in which the occurrences of old have been replaced with new, optionally restricting the number of replacements to max.

  • old -- This is old substring to be replaced.

  • new -- This is new substring, which would replace old substring.

  • max -- If this optional argument max is given, only the first count occurrences are replaced.


str.replace(old, new[, max])



str = "this is string!!! this is really string";
print str.replace("is", "was")
print str.replace("is", "was", 3)

Output / Return Value

When we run above program, it produces following result −

thwas was string!!! thwas was really string thwas was string!!! thwas is really string


Alternatives / See Also