Feb-11-2020, 04:43 PM
Since you split the string on ".", you need to reinsert the periods. Changing cleaned_str to a list and using str.join() will get that done.
import re str_to_clean = "This is an example of a paragraph that I have. I would like to remove any sentences containing certain words, for example the word bad, or naughty. If it has bad, I don't want it. If it is naughty, I do not want it. If it doesn't, I want to keep it." cleaned_str = [] for sentence in str_to_clean.split("."): if not (re.search("bad|naughty", sentence, flags=re.IGNORECASE)): cleaned_str.append(sentence) print(".".join(cleaned_str))