Find centralized, trusted content and collaborate around the technologies you use most.
Teams
Q&A for work
Connect and share knowledge within a single location that is structured and easy to search.
if var is not None and var !="" and var !=" ": # todo
我可以这样写吗?:
if var: # todo
var 只是 String 类型。
如果要过滤掉仅包含空格的字符串 ( " "):
" "
if var and var.strip(): # ...
因为如果用作谓词,包含空格的字符串将被评估为 True:
>>> bool("") False >>> bool(" ") True