2
检查字符串是否只有白色字符(如\ r \ t \ n“”)的最简单方法是什么?检查字符串中是否只有白色字符
检查字符串是否只有白色字符(如\ r \ t \ n“”)的最简单方法是什么?检查字符串中是否只有白色字符
对字符串的isspace()
方法告诉你:
>>> " ".isspace()
True
>>> " x ".isspace()
False
另一种方法是剥离端:
if not s.strip():
print "It was all whitespace!"
'foo.trim()== ''' – 2013-02-18 00:58:22
我会用一个正则表达式('re')来搜索你想要的字符/值。 – 2013-02-18 00:58:59
@AlbertoPonte在这样的任务上使用正则表达式应该被认为是犯罪 – 2013-02-18 01:05:06