您好,欢迎来到筏尚旅游网。
搜索
您的当前位置:首页Python判断字符串是否为空和null方法实例

Python判断字符串是否为空和null方法实例

来源:筏尚旅游网
Python判断字符串是否为空和null⽅法实例

判断python中的⼀个字符串是否为空,可以使⽤如下⽅法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();

以上就是本⽂的全部内容,希望对⼤家的学习有所帮助,也希望⼤家多多⽀持。

因篇幅问题不能全部显示,请点此查看更多更全内容

Copyright © 2019- efsc.cn 版权所有 赣ICP备2024042792号-1

违法及侵权请联系:TEL:199 1889 7713 E-MAIL:2724546146@qq.com

本站由北京市万商天勤律师事务所王兴未律师提供法律服务