You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Python如何判断输入内容是否包含Hello、hello、Hi指定字符串

问题原因
  • 你写的两段代码逻辑错误核心是:in 运算符左值为列表时,是判断整个列表对象是否存在于右值的字符串中,该逻辑永远不可能成立,因此永远返回False。
正确实现

写法1:完全匹配你指定的三个关键词

hello = ["Hello", "hello", "Hi"]
msg = input()
# 遍历判断任意一个关键词是否存在于输入内容中
if any(key in msg for key in hello):
    # 此处填写匹配成功后的业务逻辑
    print("检测到目标字符串")

写法2:简化版(忽略大小写,无需重复写大小写不同的同一关键词)

如果你的需求实际是不区分大小写匹配hello、hi,可简化为如下写法,同时还能匹配HELLO、hI等更多变体:

msg = input().lower()
if any(key in msg for key in ["hello", "hi"]):
    print("检测到目标字符串")

内容的提问来源于stack exchange,提问作者ItIzYe

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.10.07 11:15:00