I'm using a script to search titles on reddit posts for specific keywords. It takes a list into a for loop for comparison. I'd like a way for the loop to accept a space character, or nothing.

Say the word I'm searching for is "at". Right now, if a post comes up as "my hat", the script would accept that, which it shouldn't. If I add spaces to " at ", then the post would have to have those spaces. The sentence "at the park" wouldn't be accepted as it doesn't have a space at the beginning.

triggers = ['at', 'example', 'test']
normalized_title = submission.title.lower()
for trigger_word in triggers:
   if trigger_word in normalized_title:

I want the result to allow the word to be at the beginning or end of the post, or have space around it, but catch it if there are letters around it.

This got marked as a duplicate, but it's different. The other post just asks how to search for a specific word, which I've already got working. I'm asking about working with the syntax to get different results.

0 Answers