首页 > 解决方案 > 如何检查字符串是否在列表中,使用每个项目上的函数(python3)

问题描述

我正在制作一个类似于 AI 的程序,它会做出响应。我开始编写这个项目,但遇到了障碍。我想获取一个单词列表,然后在返回布尔值时检查给定字符串是否在列表中。我的代码看起来像这样......

listofhellos = ['hello', 'hi', 'hey!!!']
if listofhellos.lower() == 'hello': 
# This checks if string 'hello' is in listofhellos
    print('Hey, how are you?')
    # if it is in the list then print out a response

谢谢你!

标签: pythonstring

解决方案


这是解决方案:

listofhellos = ['hello', 'hi', 'hey!!!']
check = "Hello"
if check.lower() in listofhellos:
    print('Exists')
else:
    print("Not exists")

推荐阅读