如何从Python元组中grep特定关键字?
如果您有一个字符串元组,并且想要搜索特定的字符串,则可以使用in操作符。
示例
tpl = ("Hello", "world", "Foo", "bar")
print("world" in tpl)
输出
这将给出输出 –
True
示例
如果您想检查是否存在子字符串。您可以循环遍历元组,然后使用以下内容找到它:
tpl = ("Hello", "world", "Foo", "bar")
for i in tpl:
if "orld" in i:
print("在 " + i + " 找到 orld" )
输出
这将给出输出 –
在 world 找到 orld
更多Python相关文章,请阅读:Python 教程