Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

If either needle in haystack

Tags:

python

I know I can do:

if 'hello' in 'hello world':

What if I have a several needles like ('.css', '.js', '.jpg', '.gif', '.png', '.com') and I want to check if either of these is in a string.

(note: endswith won't do it in this case, they may not be suffixes)

like image 835
Juicy Avatar asked Jan 30 '26 21:01

Juicy


1 Answers

You may find any useful:

haystack = 'hello world'
needles = ['.css', '.js', '.jpg', '.gif', '.png', '.com']
if any(needle in haystack for needle in needles):
    pass  # ...
like image 95
icktoofay Avatar answered Feb 01 '26 10:02

icktoofay



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!