如何從 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
廣告