Copyright © tutorialspoint.com
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
str.replace(old, new[, max]) |
Here is the detail of parameters:
old: old substring to be replaced.
new: new substring which would replace old substring.
max: if the optional argument max is given, only the first count occurrences are replaced.
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.
#!/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 |
Copyright © tutorialspoint.com