Back

Explore Courses Blog Tutorials Interview Questions
0 votes
2 views
in Python by (47.6k 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 (106k 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')

Related questions

0 votes
1 answer
0 votes
4 answers
0 votes
1 answer
0 votes
1 answer

Browse Categories

...