Python判斷字符串是否為空和null方法實(shí)例
判斷python中的一個(gè)字符串是否為空,可以使用如下方法
1、使用字符串長度判斷
len(s) ==0 則字符串為空
#!/user/local/python/bin/python# coding=utf-8test1 = ’’if len(test1) == 0: print ’字符串TEST1為空串’else: print ’字符串TEST1不是空串,TEST1:’ + test1
2、isspace判斷是否字符串全部是空格
Python isspace() 方法檢測字符串是否只由空格組成。
#!/user/local/python/bin/python# coding=utf-8str = ' '; print str.isspace();str = 'This is string example....wow!!!';print str.isspace();TrueFalse
3、字符串去空格及去指定字符
去兩邊空格:str.strip()
去左空格:str.lstrip()
去右空格:str.rstrip()
#!/user/local/python/bin/python# coding=utf-8str = ' '; print str.strip();str = 'This is string example....wow!!! ';print str.strip();
以上就是本文的全部內(nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持好吧啦網(wǎng)。
相關(guān)文章:
1. Python調(diào)用接口合并Excel表代碼實(shí)例2. ASP.NET MVC實(shí)現(xiàn)橫向展示購物車3. ASP.Net Core(C#)創(chuàng)建Web站點(diǎn)的實(shí)現(xiàn)4. ASP.Net Core對(duì)USB攝像頭進(jìn)行截圖5. 通過CSS數(shù)學(xué)函數(shù)實(shí)現(xiàn)動(dòng)畫特效6. 一文透徹詳解.NET框架類型系統(tǒng)設(shè)計(jì)要點(diǎn)7. ASP.NET MVC使用Boostrap實(shí)現(xiàn)產(chǎn)品展示、查詢、排序、分頁8. ajax動(dòng)態(tài)加載json數(shù)據(jù)并詳細(xì)解析9. Python快速將ppt制作成配音視頻課件的操作方法10. .net如何優(yōu)雅的使用EFCore實(shí)例詳解
