Python string replacement is a problem that is often encountered when python operates on strings. here we will briefly introduce the string replacement method. python string replacement is a problem that is often encountered when python operates on strings, here is a brief introduction to the string replacement method.
Python string replacement can be implemented in two ways:
1 is the method of using the string itself.
2. replace strings with regular expressions.
The following example is used for an experiment:
A = 'Hello word'
Replace the word in string a with python
1. use the replace method of the string itself
The code is as follows:
A. replace ('word', 'Python ')
The output is hello python.
2. replace with a regular expression:
The code is as follows:
Import re
Strinfo = re. compile ('word ')
B = strinfo. sub ('Python',)
Print B
The output is also hello python.
If you use this method, it depends on your choice.