可以使用 isspace() 方法来判断一个字符串是否全部是空格。isspace() 方法会检测字符串中的所有字符是否都是空格字符,如果是则返回 True,否则返回 False

下面是一个示例代码:

def is_all_spaces(s):
    return s.isspace()

# 测试
print(is_all_spaces('   '))  # True
print(is_all_spaces('  hello  '))  # False
print(is_all_spaces(''))  # False
Python 判断字符串是否全为空格:isspace() 方法详解

原文地址: https://www.cveoy.top/t/topic/P1c 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录