如何从 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("Found orld in " + i )输出
这将输出 -
Found orld in world
广告
数据结构
网络
关系数据库管理系统
操作系统
Java
iOS
HTML
CSS
Android
Python
C 编程
C++
C#
MongoDB
MySQL
Javascript
PHP