3

检查字符串是否只有白色字符(如 \r \t \n " ")的最简单方法是什么?

4

1 回答 1

10

isspace()字符串方法告诉你:

>>> "   ".isspace()
True
>>> " x  ".isspace()
False

另一种选择是剥去末端:

if not s.strip():
    print "It was all whitespace!"
于 2013-02-18T01:03:46.550 回答