Latest web development tutorials

Python replace () method

Python strings Python strings


description

Python replace () method to a string of old (old string) replaced new (new string), if you specify the third parameter max, then replace no more than max times.

grammar

replace () method syntax:

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

parameter

  • old - will be replaced with the substring.
  • new - new string, which replaces old substring.
  • max - optional string to replace no more than max times

return value

Returns a string of old (old string) replaced with a new string new (new String) after generation, if you specify the third parameter max, then replace no more than max times.

Examples

The following example shows the replace () method using the function:

#!/usr/bin/python

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

Examples of the above output results are as follows:

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

Python strings Python strings