检查一个字符串是否只有python中的白色字符

Raf*_*l T 2 python string

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

Ned*_*der 8

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

>>> "   ".isspace()
True
>>> " x  ".isspace()
False
Run Code Online (Sandbox Code Playgroud)

另一种选择是剥去两端:

if not s.strip():
    print "It was all whitespace!"
Run Code Online (Sandbox Code Playgroud)