jahmax jahmax - 1 year ago 141
Python Question

Check if multiple strings exist in another string

How can I check if any of the strings in an array exists in another string?


a = ['a', 'b', 'c']
str = "a123"
if a in str:
print "some of the strings found in str"
print "no strings found in str"

That code doesn't work, it's just to show what I want to achieve.

Answer Source

You can use any:

if any(x in str for x in a):

Similarly to check if all the strings from the list are found, use all instead of any.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download