I want to delete certain words from a paragraph, such as "and", "as", and "like". Is there an easier way to delete words from a string than doing it via replace --
new_str = str.replace(' and ', '').replace(' as ', '').replace(' like ', '')
For example, is there a method similar to the following?
str.remove([' and ', ' like ', ' as '])
Yes, you could use the sub
function from the re
module:
>>> import re
>>> s = 'I like this as much as that'
>>> re.sub('and|as|like', '', s)
'I this much that'
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With