'only keep certain words in a string python

I was wondering if there was a way to strip away text that isn't the text I needed. I need to remove all text from a screenshot that isn't "loud noises". I've searched all over, but I haven't found an answer. My code:

if "loud noises" in text:
    print(text)
else:
    pass

I need a way to only display the text "loud noises", and nothing else. (it's a long story)



Solution 1:[1]

what you could do is:

def getWord(idx,word,get):
    try:
        total = 0
        for i in range(len(get)):
            if word[idx + i] == get[i]:
                total += 1
        if total == len(get):
            return True
        else:
            return False
    except IndexError:
        # out of range
        return
text = "I make loud noises"
newtext = ""
for idx in range(len(text)):
    if getWord(idx,text,"loud noises"):
        newtext += "loud noises"

print(newtext)

It accomplishes what you're thinking of, and could be used as mehtod.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 aanginer