Python 官方文档:入门教程 => 点击学习
本篇文章给大家分享的是有关python中怎么判断字符串是否为空和null,小编觉得挺实用的,因此分享给大家学习,希望大家阅读完这篇文章后可以有所收获,话不多说,跟着小编一起来看看吧。1、使用字符串长度判断len(s) ==0 则字符串为空#
本篇文章给大家分享的是有关python中怎么判断字符串是否为空和null,小编觉得挺实用的,因此分享给大家学习,希望大家阅读完这篇文章后可以有所收获,话不多说,跟着小编一起来看看吧。
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-8 str = " "; 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();
以上就是Python中怎么判断字符串是否为空和null,小编相信有部分知识点可能是我们日常工作会见到或用到的。希望你能通过这篇文章学到更多知识。更多详情敬请关注编程网Python频道。
--结束END--
本文标题: Python中怎么判断字符串是否为空和null
本文链接: https://www.lsjlt.com/news/228034.html(转载时请注明来源链接)
有问题或投稿请发送至: 邮箱/279061341@qq.com QQ/279061341
下载Word文档到电脑,方便收藏和打印~
2024-03-01
2024-03-01
2024-03-01
2024-02-29
2024-02-29
2024-02-29
2024-02-29
2024-02-29
2024-02-29
2024-02-29
回答
回答
回答
回答
回答
回答
回答
回答
回答
回答
0