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.
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).
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.
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.
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 ).
Use any()
:
if any(s in some_string for s in example):
# at least one of the elements is a substring of some_string
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With