Latest web development tutorials

Python3 replace () method

Python3 string Python3 string


description

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/python3

str = "www.w3cschool.cc"
print ("本教程新地址:", str)
print ("本教程新地址:", str.replace("w3cschool.cc", "w3big.com"))

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

Examples of the above output results are as follows:

本教程新地址: www.w3cschool.cc
本教程新地址: www.w3big.com
thwas was string example....wow!!!

Python3 string Python3 string