Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/loops/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在python中查找字符串中的字符串元组_Python_Python 3.x - Fatal编程技术网

在python中查找字符串中的字符串元组

在python中查找字符串中的字符串元组,python,python-3.x,Python,Python 3.x,使用startswith()和endswith()函数,可以传递字符串元组,如果任何元素上存在匹配项,则返回结果True,例如: text_to_find = ("string_1", "string2") test = "string_to_look_in" if test.startswith(text_to_find ) == True: print ("starts with one of the strings") 是否有一个类似的命令,将用于元组,用于在字符串中查找

使用
startswith()
endswith()
函数,可以传递字符串元组,如果任何元素上存在匹配项,则返回结果
True
,例如:

text_to_find = ("string_1", "string2")

test = "string_to_look_in"

if test.startswith(text_to_find ) == True:
     print  ("starts with one of the strings")

是否有一个类似的命令,将用于元组,用于在字符串中查找字符串-即,元组中的一个字符串出现在文本中的任何位置。(例如,不要使用for循环,即单独查看字符串中的每个项目)。

首先,尝试使用
any
测试列表中的所有项目。其次,由于您想知道字符串是否包含在文本中的任何位置,因此可以在中使用
。例如:

text_to_find = ("string_1", "string2", "to_look")

test = "string_to_look_in"

if any(s in test for s in text_to_find):
     print  ("contains one of the strings")

首先,尝试使用
any
测试列表中的所有项目。其次,由于您想知道字符串是否包含在文本中的任何位置,因此可以在
中使用
。例如:

text_to_find = ("string_1", "string2", "to_look")

test = "string_to_look_in"

if any(s in test for s in text_to_find):
     print  ("contains one of the strings")

我建议
如果有(文本中x的x在测试中要查找):
。测试布尔条件时不需要
==True
。@khelwood“答案”是
如果有的话(在文本中x的测试中x表示x要查找)
”。元组中的一个字符串出现在文本中的任何地方。@Jean Françoisfare您完全正确。在发表我的评论之前,我没有注意到问题的“任何地方”部分。但是你已经了解了大概的意思。我建议
如果有(文本中x的x的测试中的x要查找):
。测试布尔条件时不需要
==True
。@khelwood“答案”是
如果有的话(在文本中x的测试中x表示x要查找)
”。元组中的一个字符串出现在文本中的任何地方。@Jean Françoisfare您完全正确。在发表我的评论之前,我没有注意到问题的“任何地方”部分。但是你知道了大概的意思。