Use of the string replace () method in Python, pythonreplace
The replace () method returns the replace of the current old with new, which can be used to replace the copy of the maximum number of strings.
Syntax
The syntax of the replace () method is as follows:
Str. replace (old, new [, max])
Parameters
- Old -- this is the old substring to be replaced.
- New -- this is a new substring that replaces the old substring.
- Max -- if the max value of this optional parameter is given, only the first count is replaced.
Return Value
This method returns the replace between the copy of the string and the old substring. If the maximum value of the optional parameter is specified, only the first count is replaced.
Example
The following example demonstrates the use of the replace () method.
#! /Usr/bin/pythonstr = "this is string example... wow !!! This is really string "; print str. replace (" is "," was "); print str. replace (" is "," was ", 3 );
When we run the above program, it will produce the following results:
Thwas was string example... wow !!! Thwas was really stringthwas was string example... wow !!! Thwas is really string