Multiple value checks using 'in' operator (Python)

前端 未结 7 1679
我寻月下人不归
我寻月下人不归 2020-11-29 07:37
if \'string1\' in line: ...

... works as expected but what if I need to check multiple strings like so:

if \'string1\' or \'string2         


        
7条回答
  •  無奈伤痛
    2020-11-29 08:22

    or does not behave that way. 'string1' or 'string2' or 'string3' in line is equivalent to ('string1') or ('string2') or ('string3' in line), which will always return true (actually, 'string1').

    To get the behavior you want, you can say if any(s in line for s in ('string1', 'string2', 'string3')):.

提交回复
热议问题