python - 检查字符串是否仅包含空格

如何测试字符串是否只包含空格?

示例字符串:

  • ""(空格、空格、空格)

  • "\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/

相关文章:

linux - 强制卸载 NFS 挂载的目录

python - django 导入错误 - 没有名为 core.management 的模块

linux - 解压目录中的所有文件

python - 如何重命名 conda 环境?

python - 您如何以编程方式设置属性?

python - 如何覆盖 Python 中的 [] 运算符?

python - 如何从生成器中只选择一项?

linux - ld 找不到现有的库

linux - 对制表符分隔的文件进行排序

linux - httpd : Could not reliably determine the s