is there a way to do something like this?
if ['hel','ell','orl'] in 'hello world' :
I want to see if all of these strings occur in the word. If possible in a shorter way than completely writing a multiline foor loop.
if all(substr in 'hello world' for substr in ('hel','ell','orl')):
# all are contained
The advantage of all()
is that it stops checking as soon as one substr
does not match.
A multiline for
loop is the correct way to proceed. If you don't like how it looks in your code extract it into a function and then you just have a single line function call for the same thing.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.