How can I check if any of the strings in an array exists in another string?
For example:
JavaScript
x
7
1
a = ['a', 'b', 'c']
2
s = "a123"
3
if a in s:
4
print("some of the strings found in s")
5
else:
6
print("no strings found in s")
7
How can I replace the if a in s:
line to get the appropriate result?
Advertisement
Answer
You can use any
:
JavaScript
1
5
1
a_string = "A string is more than its parts!"
2
matches = ["more", "wholesome", "milk"]
3
4
if any([x in a_string for x in matches]):
5
Similarly to check if all the strings from the list are found, use all
instead of any
.