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

10年積累的做網(wǎng)站、成都網(wǎng)站制作經(jīng)驗(yàn),可以快速應(yīng)對(duì)客戶對(duì)網(wǎng)站的新想法和需求。提供各種問(wèn)題對(duì)應(yīng)的解決方案。讓選擇我們的客戶得到更好、更有力的網(wǎng)絡(luò)服務(wù)。我雖然不認(rèn)識(shí)你,你也不認(rèn)識(shí)我。但先建設(shè)網(wǎng)站后付款的網(wǎng)站建設(shè)流程,更有安源免費(fèi)網(wǎng)站建設(shè)讓你可以放心的選擇與我們合作。
1、使用字符串長(zhǎng)度判斷
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() 方法檢測(cè)字符串是否只由空格組成。
#!/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();
網(wǎng)站名稱:Python中判斷字符串是否為空和null常用的方法
路徑分享:http://m.fisionsoft.com.cn/article/dhjjhog.html


咨詢
建站咨詢
