isspace()方法是用于字符串的,它会告诉你以下信息:>>> " ".isspace() True >>> " x ".isspace() False 另一个选项是去掉两端:if not s.strip(): print "It was all whitespace!"
not foo.strip()
(在Python中不存在str.trim()
,而与空字符串比较最好用not
替换),虽然这仍然不是最佳选项。 - Gareth Latty