我有一个cookie值,是通过Python的POST调用返回的。
我需要检查cookie
值是否为空或null。
因此,我需要一个if条件的函数或表达式。
如何在Python中实现这一点?
例如:
if cookie == NULL
if cookie == None
顺便提一下,cookie
是存储值的变量。
试试这个:
if cookie and not cookie.isspace():
# the string is non-empty
else:
# the string is empty
以上考虑了字符串为 None
或仅由空格组成的情况。
cookie.isspace()
比使用strip
更高效。 - John La Rooybool(sequence)
如果序列为空则为False
。由于字符串是序列,因此可以这样使用:cookie = ''
if cookie:
print "Don't see this"
else:
print "You'll see this"
if cookie
和 if cookie == ""
之间的功能差异是什么? - NoNameNone
或为空(包含空格),例如 ' '
或 '\n\t'
。 - StickySli
None
值?仅空字符串?)。 - Eric O. Lebigot