describes the
replace () method to replace old (old string) in the string into new (new string). If the third parameter Max is specified, the replacement is no more than max times.

replace () method syntax:

str.replace (old, new[, max])


-- the substring that will be replaced.
new - a new string that is used to replace the old substring.
Max -- the optional string is replaced by no more than max times
return value.
returns the new string generated by the old in the string (old string) instead of new (new string). If the third parameter Max is specified, then the replacement is no more than max times.

examples of
following example shows (replace) function is used:

 /usr/bin/python3 STR = "#! Welcome to visit the print script" ("the old script home address:" STR "(print) script home new address:" str.replace ("", "" STR)) = "this is string!!!" (print str.replace ("is", "was", 3)) 

above example output results are as follows:

script home old address:
script home new address:
thwas was string example.Wow...!! When

! Python replace

replace can replace string replacement string, need to assign a value to a variable, because the string in Python is an immutable object, So we have to reassign it when we use it, and it's so simple. There are two kinds of replace

Python method


last_date = "1/2/3" target as "one of 123"

: repalce

date =last_date.replace

('/', '')

two: re

 P = re.compile ("/") date = p.sub (last_date ', 

) Note:


must not escape, otherwise the function will not take effect.

returns the text string replacement after regular expression based replication.

stringObj.replace (rgExp, replaceText)

parameters stringObj
required. To execute the String object or string literal for the replacement. This string will not be modified by the replace method.
required. A regular expression object that contains a regular expression pattern or a usable flag. It can also be a String object or a text. If rgExp is not a regular expression object, it will be converted to a string, and exact search; don't try to string into regular expressions.
required. It is a String object or string literal that is replaced by the text contained in the object for each matching rgExp in the stringObj. In Jscript 5.5 or later, replaceText parameters can also be function returns the text. For example:

content.replace ('n'').Replace ('r'').Replace (.Replace ('\\' ',) ','

'trailing' .) you might be interested in this article:


This concludes the body part

This paper fixed link: | Script Home | +Copy Link

Article reprint please specify:The method of using Python3 replace () function | Script Home

You may also be interested in these articles!