确定字符串是否包含单词 [英] Determining if a string contains a word

查看:87
本文介绍了确定字符串是否包含单词的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在Python中,给定以下上下文的语句的语法是什么:

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

words = 'blue yellow'

将是一个if语句,用于检查words是否包含单词"blue"吗?即

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."

推荐答案

words = 'blue yellow'

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

编辑

我刚刚意识到nightly blues将包含blue,但不是一个整体.如果这不是您想要的,请拆分单词列表:

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():
    …

这篇关于确定字符串是否包含单词的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆