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

作者:道法自然﹑ 时间:2021-06-27 07:47:23 

判断python中的一个字符串是否为空,可以使用如下方法

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();

来源:https://www.cnblogs.com/hello-wei/p/12504548.html

标签:Python,字符串,空,null
0
投稿

猜你喜欢

  • Python工程师面试题 与Python Web相关

    2021-11-10 13:00:48
  • Golang中panic的异常处理

    2023-10-13 12:27:23
  • js给静态网页代码加密方法

    2007-08-04 19:48:00
  • Python断言assert的用法代码解析

    2021-10-05 21:57:07
  • ASP 生成静态新闻列表

    2009-03-03 12:25:00
  • Python3.x和Python2.x的区别介绍

    2022-01-05 12:36:27
  • PyTorch中的Variable变量详解

    2023-02-19 18:48:47
  • 用python实现前向分词最大匹配算法的示例代码

    2023-12-03 00:18:46
  • Python的Flask项目中获取请求用户IP地址 addr问题

    2021-09-08 08:38:47
  • Python实现将mp3音频格式转换为wav格式

    2023-09-18 15:50:02
  • Python实现为图片添加水印的示例详解

    2023-11-14 09:49:09
  • 迎来2009年CSS裸奔节(CSS Naked Day )

    2009-04-24 12:41:00
  • Python实现导出数据生成excel报表的方法示例

    2021-05-17 16:43:57
  • python使用装饰器和线程限制函数执行时间的方法

    2022-04-03 04:06:34
  • Tornado Web服务器多进程启动的2个方法

    2022-01-21 04:41:05
  • Python Tkinter 简单登录界面的实现

    2021-01-10 20:45:03
  • SQL Server应用程序的高级Sql注入

    2009-04-11 16:54:00
  • 浅析Python面向对象编程

    2023-11-23 07:52:12
  • 在python 脚本下解析json数据

    2022-05-28 10:38:20
  • python实现矩阵乘法

    2023-11-03 07:41:10
  • asp之家 网络编程 m.aspxhome.com