如何测试字符串是否只包含空格?
示例字符串:
""
(空格、空格、空格)
"\t\n "
(空格、制表符、空格、换行符、空格)
"\n\n\n\t\n"
(换行符、换行符、换行符、制表符、换行符)
最佳答案
使用 str.isspace()
方法:
Return
True
if there are only whitespace characters in the string and there is at least one character,False
otherwise.A character is whitespace if in the Unicode character database (see unicodedata), either its general category is Zs (“Separator, space”), or its bidirectional class is one of WS, B, or S.
将其与处理空字符串的特殊情况结合起来。
或者,您可以使用 str.strip()
并检查结果是否为空。
https://stackoverflow.com/questions/2405292/