[Tutor] How to find a word in a string

Phil phillor9 at gmail.com
Mon May 3 21:09:38 EDT 2021


This is a bit trickier that I had at first thought, for example:

test = 'Do this, then do that.'

if 'this' in test.lower().split():
     print('found')
else:
     print('not found')

Does not find 'this' because of the comma.

I had experimented with .find() but that's not the answer either because 
if I was searching a sentence for 'is' it would be found in 'this' 
rather than only 'is' as a word. I also thought about striping all 
punctuation but that seems to be unnecessarily complicated.

So, how I might I deal with the comma in this case?

-- 
Regards,
Phil



More information about the Tutor mailing list