新聞中心
這里有您想知道的互聯(lián)網(wǎng)營銷解決方案
Python中判斷字符串是否為空和null常用的方法
本篇文章重點為大家講解一下Python中字符串是否為空和null常用的方法,有需要的小伙伴可以參考一下。

10年積累的做網(wǎng)站、成都網(wǎng)站制作經(jīng)驗,可以快速應(yīng)對客戶對網(wǎng)站的新想法和需求。提供各種問題對應(yīng)的解決方案。讓選擇我們的客戶得到更好、更有力的網(wǎng)絡(luò)服務(wù)。我雖然不認識你,你也不認識我。但先建設(shè)網(wǎng)站后付款的網(wǎng)站建設(shè)流程,更有安源免費網(wǎng)站建設(shè)讓你可以放心的選擇與我們合作。
1、使用字符串長度判斷
len(s) ==0 則字符串為空
#!/user/local/python/bin/python
# coding=utf-8
test1 = ''
if len(test1) == 0:
print '字符串TEST1為空串'
else:
print '字符串TEST1不是空串,TEST1:' + test1
2、isspace判斷是否字符串全部是空格
Python isspace() 方法檢測字符串是否只由空格組成。
#!/user/local/python/bin/python
# coding=utf-8
str = " ";
print str.isspace();
str = "This is string example....wow!!!";
print str.isspace();
True
False
3、字符串去空格及去指定字符
去兩邊空格:str.strip()
去左空格:str.lstrip()
去右空格:str.rstrip()
#!/user/local/python/bin/python
# coding=utf-8
str = " ";
print str.strip();
str = "This is string example....wow!!! ";
print str.strip();
文章名稱:Python中判斷字符串是否為空和null常用的方法
網(wǎng)頁路徑:http://www.5511xx.com/article/dhjjhog.html


咨詢
建站咨詢
