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