Copyright © tutorialspoint.com

Python String replace() Method

previous next


Description:

This method 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

Syntax:

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

Parameters:

Here is the detail of parameters:

Return Value:

It returns a copy of the string with all occurrences of substring old replaced by new. If the optional argument max is given, only the first count occurrences are replaced.

Example:

#!/usr/bin/python

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

This will produce following result:

thwas was string example....wow!!! thwas was really string
thwas was string example....wow!!! thwas is really string

previous next

Copyright © tutorialspoint.com