[Solved] How many times words (from list) do occur in string [python]


If you want to check for substrings also i.e azy in lazy, you need to check each word from words is in each substring of message:

message = "The quick brown fox jumps over the lazy dog"

words = ["the","over","azy","dog"]
print(sum(s in word for word in set(message.lower().split()) for s in words))
4

Or simply check if each word from words is contained in the string:

print(sum(word in message for word in words))
4

You also need to call lower on the string if you want to ignore case.

solved How many times words (from list) do occur in string [python]