如何在python中检查字符串是否为null

San*_*nan 12 python string null

我有一个使用Python从POST调用返回的值cookie.
我需要检查cookie值是空还是null.因此,我需要if条件的函数或表达式.我怎么能用Python做到这一点?例如:

if cookie == NULL

if cookie == None
Run Code Online (Sandbox Code Playgroud)

PS cookie是存储值的变量.

Ósc*_*pez 19

试试这个:

if cookie and not cookie.isspace():
    # the string is non-empty
else:
    # the string is empty
Run Code Online (Sandbox Code Playgroud)

以上考虑了字符串是None或一系列空格的情况.


mgi*_*son 5

在 python 中,bool(sequence)如果False序列为空。由于字符串是序列,因此这将起作用:

cookie = ''
if cookie:
    print "Don't see this"
else:
    print "You'll see this"
Run Code Online (Sandbox Code Playgroud)