Python3 replace() Method
Description
The replace() method replaces old (the old string) with new (the new string) in a string; if the third parameter max is specified, no more than max replacements are made.
Syntax
Syntax of the replace() method:
str.replace(old, new[, max])
Parameters
- old -- the substring to be replaced.
- new -- the new string used to replace the old substring.
- max -- optional string, no more than max replacements
Return Value
Returns the new string generated after replacing old (the old string) with new (the new string) in the string; if the third parameter max is specified, no more than max replacements are made.
Example
The following example shows how to use the replace() function:
Example
#!/usr/bin/python3
str
=
"
www.w3cschool.cc
"
(
"
Runoob Tutorial old address:
"
,
str
)
(
"
Runoob Tutorial new address:
"
,
str
.
replace
(
"
w3cschool.cc
"
,
"
runoob.com
"
)
)
str
=
"
this is string example....wow!!!
"
(
str
.
replace
(
"
is
"
,
"
was
"
,
3
)
)
The output of the above example is:
Runoob Tutorial旧地址: www.w3cschool.cc
Runoob Tutorial新地址: www.runoob.com
thwas was string example....wow!!!
Python3 Strings