【问题标题】:How to check that a string is not empty in python/only contains whitespace? [duplicate]如何检查python中的字符串是否为空/仅包含空格? [复制]
【发布时间】:2020-09-14 08:39:59
【问题描述】:

我在 python 中有一个名为“string”的变量。如何检查我的变量是否仅包含空格,但其中包含实际字符? 如果变量只包含空格,我不想打印它。如果它有一个字符,那么我想打印变量。

【问题讨论】:

  • 你可以这样做:if line.strip()

标签: python whitespace


【解决方案1】:

使用isspace():

string = ' '
if not string.isspace():
  print(string)

【讨论】:

    猜你喜欢
    • 2011-01-25
    • 2011-01-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-08-17
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多