Use of the replace () method to operate strings in Python
This article describes how to use the replace () method to operate strings in Python. It is a basic knowledge in Python. For more information, see
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:
?
1 |
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.
?
1 2 3 4 5 |
#! /Usr/bin/python Str = "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:
?
1 2 |
Thwas was string example... wow !!! Thwas was really string Thwas was string example... wow !!! Thwas is really string |