spootnx spootnx - 6 months ago 12
Python Question

How to subtract multiple string values from another string

With:

abc = 'abc'
xyz = 'xyz'

word = 'begin abc- middle_xyz_ end'


I need to extract the values of
abc
and
xyz
from
word
.
The result should be

result = 'begin - middle__ end'


How to achieve this with a minimum amount of code?

Answer

You use replace() with an empty string as the value to replace with.

result = word.replace('abc','').replace('xyz','')
Comments