Describe
The Python replace () method replaces the old (older string) in the string with the new one, and if you specify the third parameter max, the replacement does not exceed Max times.
Grammar
Replace () method syntax:
Str.replace (old, new[, Max])
Parameters
- Old--the substring to be replaced.
- New-A string that replaces the old substring.
- Max--Optional string, replace no more than Max Times
return value
Returns a new string that is generated after the old (older string) in the string is replaced with new (the newly created string), and if the third parameter max is specified, the substitution does not exceed Max times.
Instance
The following example shows how to use the Replace () function:
#!/usr/bin/pythonstr = "This is a string EXAMPLE....WOW!!! This is really string ";p rint str.replace (' is ', ' was ');p rint str.replace (" is "," was ", 3);
The result of the above example output is as follows:
Thwas was string Example....wow!!! Thwas was really stringthwas is string EXAMPLE....WOW!!! Thwas is really string
Transferred from: http://www.runoob.com/python/att-string-replace.html
Recently began to learn Python, wrote an XSS scanner, encountered a lot of problems, in the following days, I will put these insights and read the information written or loaded into the blog, just as a way to consult later.
Learn a little every day!
Python Replace () method