[Solved] Python – Check if a word is in a string [duplicate]


What about to split the string and strip words punctuation, without forget the case?

w in [ws.strip(',.?!') for ws in p.split()]

Maybe that way:

def wordSearch(word, phrase):
    punctuation = ',.?!'
    return word in [words.strip(punctuation) for words in phrase.split()]

    # Attention about punctuation (here ,.?!) and about split characters (here default space)

Sample:

>>> print(wordSearch('Sea'.lower(), 'Do seahorses live in reefs?'.lower()))
False
>>> print(wordSearch('Sea'.lower(), 'Seahorses live in the open sea.'.lower()))
True

I moved the case transformation to the function call to simplify the code…
And I didn’t check performances.

1

solved Python – Check if a word is in a string [duplicate]