Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python check if any items of a tuple are in a string [duplicate]

Tags:

python

ubuntu

Possible Duplicate:
Check if multiple strings exist in another string
Check to ensure a string does not contain multiple values

So guys. If i have

example = "1", "2", "3"

How would i check if any of the items are in a string.

like image 231
user1705279 Avatar asked Sep 28 '12 18:09

user1705279


People also ask

How do you check if elements of a tuple are same?

When it is required to check if two list of tuples are identical, the '==' operator is used. The '==' operator checks to see if two iterables are equal or not. A list can be used to store heterogeneous values (i.e data of any data type like integer, floating point, strings, and so on).

How do you check if a tuple contains a string in Python?

Method 1: By using a loop: If any value in the tuple is equal to the given value, it will return True. Else, it will return False. Here, contains method is used to check if a character is in a given tuple or not.

How do I check if a string contains multiple substrings?

Use the any() function to check if multiple strings exist in another string, e.g. if any(substring in my_str for substring in list_of_strings): . The any() function will return True if at least one of the multiple strings exists in the string.

How do you check if a list of substrings is in a string Python?

The easiest way to check if a Python string contains a substring is to use the in operator. The in operator is used to check data structures for membership in Python. It returns a Boolean (either True or False ).


1 Answers

Use any():

if any(s in some_string for s in example):
    # at least one of the elements is a substring of some_string
like image 104
Andrew Clark Avatar answered Oct 07 '22 12:10

Andrew Clark