Multiple value checks using 'in' operator (Python)

前端 未结 7 1700
我寻月下人不归
我寻月下人不归 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:11


    1. You have this confusion Because you don't understand how the logical operator works with respect to string.

      Python considers empty strings as False and Non empty Strings as True.

      Proper functioning is :

      a and b returns b if a is True, else returns a.

      a or b returns a if a is True, else returns b.

      Therefore every time you put in a non empty string in place of string1 the condition will return True and proceed , which will result in an undesired Behavior . Hope it Helps :).

提交回复
热议问题