0 votes
1 view
in Python by (19.9k points)

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

Like:

a = ['a', 'b', 'c']

str = "a123"

if a in str:

print "some of the strings found in str"

else:

print "no strings found in str"

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

1 Answer

0 votes
by (50k points)

If you want to check whether any of the strings in an array exists in another string or not you can use the keywordany’ below is an example that shows how to use any keyword:-

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

Another thing you can do is to use the regular expression:-

import re

if any(re.findall(r'a|b|c', str, re.IGNORECASE)):

print('possible matches found')

else:

print('no matches')

...