本篇文章給大家分享的是有關Python中怎么判斷字符串是否為空和null,小編覺得挺實用的,因此分享給大家學習,希望大家閱讀完這篇文章后可以有所收獲,話不多說,跟著小編一起來看看吧。

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,小編相信有部分知識點可能是我們日常工作會見到或用到的。希望你能通過這篇文章學到更多知識。更多詳情敬請關注創新互聯-成都網站建設公司行業資訊頻道。
當前名稱:Python中怎么判斷字符串是否為空和null-創新互聯
地址分享:http://www.yijiale78.com/article40/cspjho.html
成都網站建設公司_創新互聯,為您提供關鍵詞優化、品牌網站制作、手機網站建設、營銷型網站建設、軟件開發、全網營銷推廣
聲明:本網站發布的內容(圖片、視頻和文字)以用戶投稿、用戶轉載內容為主,如果涉及侵權請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網站立場,如需處理請聯系客服。電話:028-86922220;郵箱:631063699@qq.com。內容未經允許不得轉載,或轉載時需注明來源: 創新互聯