How do we grep a particular keyword from Python tuple?


If you have a tuple of strings and you want to search for a particular string, You can use the in operator. 

example

tpl = ("Hello", "world", "Foo", "bar")
print("world" in tpl)

Output

This will give the output −

True

Example

If you want to check if there is a substring present. You can loop over the tuple and find it using:

tpl = ("Hello", "world", "Foo", "bar")
for i in tpl:
   if "orld" in i:
      print("Found orld in " + i )

Output

This will give the output −

Found orld in world

Lakshmi Srinivas
Lakshmi Srinivas

Programmer / Analyst / Technician

Updated on: 05-Mar-2020

159 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements