Python replace() method
Description
The Python replace() method replaces old (old string) in a string with new (new 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/python
str
=
"
this is string example....wow!!! this is really string
"
;
str
.
replace
(
"
is
"
,
"
was
"
)
;
str
.
replace
(
"
is
"
,
"
was
"
,
3
)
;
The output of the above example is:
thwas was string example....wow!!! thwas was really string
thwas was string example....wow!!! thwas is really string
Python Strings