You can chain 2 str.replace()
together. For this specific and very simple case:
>>> s = '1256Ab79'
>>> s.replace('56', '').replace('7', '')
'12Ab9'
>>> s = 'a567Eq79'
>>> s.replace('56', '').replace('7', '')
'aEq9'
The first replace()
replaces any instance of the sub-string '56'
with an empty string, effectively removing it. Likewise the second removes '7'
from the string. Note that we can chain multiple calls to replace()
together because replace()
returns the modified string.
If you require more complex replacements, or there are a great many possible target strings, then you could look at re.sub()
and dynamically construct the regex pattern from a list of possible target strings.
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…