Python检查多个字符串中是否有空字符串

Chr*_*ung 3 python string if-statement

我知道这是一个基本问题,但请耐心等待.假设我们下面有4个字符串:

a = ''
b = 'apple'
c = 'orange'
d = 'banana'
Run Code Online (Sandbox Code Playgroud)

所以,通常如果我想检查三个字符串中的任何一个a b c是否为空,我可以使用len()函数.

if len(a) == 0 or len(b) == 0 or len(c) == 0:
    return True
Run Code Online (Sandbox Code Playgroud)

但是后来我觉得如果我有很多字符串就像上面这样写太麻烦了.所以,我用过

if not a:
    return True
Run Code Online (Sandbox Code Playgroud)

但是,当我b c d使用上面的方法检查多个字符串时,它返回True并且我感到困惑的是没有b c d空字符串.

if not b or c or d:
    return True
Run Code Online (Sandbox Code Playgroud)

到底是怎么回事?

Jam*_*les 7

问题在于这条线:

if not b or c or d:
Run Code Online (Sandbox Code Playgroud)

您需要为每个字符串包含"not"条件.所以:

if not b or not c or not d:
Run Code Online (Sandbox Code Playgroud)

你也可以这样做:

    return '' in [a, b, c, d]
Run Code Online (Sandbox Code Playgroud)