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
Updated on: 2020-03-05T05:54:11+05:30

200 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements