Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Determining if a string contains a word [duplicate]

In Python, what is the syntax for a statement that, given the following context:

words = 'blue yellow'

would be an if statement that checks to see if words contains the word "blue"? I.e.,

if words ??? 'blue':
    print 'yes'
elif words ??? 'blue':
    print 'no'

In English, "If words contain blue, then say yes. Otherwise, print no."

like image 666
John Doe Smith Avatar asked Mar 13 '13 00:03

John Doe Smith


1 Answers

words = 'blue yellow'

if 'blue' in words:
    print 'yes'
else:
    print 'no'

Edit

I just realized that nightly blues would contain blue, but not as a whole word. If this is not what you want, split the wordlist:

if 'blue' in words.split():
    …
like image 155
slezica Avatar answered Oct 06 '22 01:10

slezica