Find multiple lines in Python

Is there a way to do something like this?

if ['hel','ell','orl'] in 'hello world' : 

I want all these lines to appear in the word. If possible, a shorter path than a full multi-line loop recording.

+4
source share
3 answers

You can do:

 if all( x in 'hello world' for x in ['hel','ell','orl'] ): print "Found all of them" 

The built-in functions all and any are useful for this kind of thing.

+11
source
 if all(substr in 'hello world' for substr in ('hel','ell','orl')): # all are contained 

The advantage of all() is that it stops being checked as soon as one substr does not match.

+4
source

A multi-line for loop is the right way to continue. If you don’t like how it looks in your code, extract it into a function, and then you will only have to call one line for the same thing.

0
source

Source: https://habr.com/ru/post/1343737/


All Articles