Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python if this and this and this

I am new to Python. I am writing a string function to check if a certain string contains certain multiple values. Is there a syntax in python to allow me to do something like this:

def strParse(str):
    a = 't'
    b = 'br'
    c = 'ht'
    if a in str AND b in str AND c in str:
        print('Ok!')

(The part I am unsure about is having multiple if statements on line.) Thanks!

like image 578
eatonphil Avatar asked Dec 17 '25 07:12

eatonphil


1 Answers

Almost correct, just make the and lower-case:

def strParse(str):
    a = 't'
    b = 'br'
    c = 'ht'
    if a in str and b in str and c in str:
        print('Ok!')

Doesn't cause problems in this case, but you should avoid using variable-names that are also built-in functions (str is a builtin function/type)

If you have more values, you can do the same thing more tidily like so:

values = ['t', 'br', 'ht']
if all(x in instr for x in values):
    print("Ok!")
like image 130
dbr Avatar answered Dec 19 '25 21:12

dbr



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!