【发布时间】:2021-02-18 22:46:14
【问题描述】:
我想编写一个函数来检查给定的句子是否包含给定的单词。例如:
my_string = 'Biden'
sentence = 'Biden is the new president of the United States.'
if my_string.lower() in sentence.lower().split():
print('Sentence contains string')
else:
print('Sentence does not contain string')
此示例将返回 True。现在,一旦字符串不仅仅是一个单词,就会出现问题。
my_string = 'Joe Biden'
sentence = 'Joe Biden is the new president of the United States.'
if my_string.lower() in sentence.lower().split():
print('Sentence contains string')
else:
print('Sentence does not contain string')
这里它会返回 False。这个问题有简单的解决方案吗?
【问题讨论】:
标签: python contains word sentence