【发布时间】:2021-07-05 16:18:19
【问题描述】:
x = input("enter input: ")
if x == "hello":
print ("it does")
即使 x 有其他字符/字符串,我如何检测 x 是否存储了 hello?
【问题讨论】:
标签: python-3.x
x = input("enter input: ")
if x == "hello":
print ("it does")
即使 x 有其他字符/字符串,我如何检测 x 是否存储了 hello?
【问题讨论】:
标签: python-3.x
如果输入的输入是单个字符串,则下面的 x 将是一个元素的数组,如果输入的输入是空格分隔的字符串(字符串的字符串),那么 x 将是多个字符串的数组,下面的代码处理这两个选项
x = input("Enter input: ").split()
for y in x:
if y=="hello"
print("it does")
【讨论】:
这就像使用 in 关键字一样简单。
x = "123hellomore"
if "hello" in x:
print("Hi there")
这仅检测hello,如果它通畅所以仍然在一个单词中(不像“**hel*lo”)
【讨论】: