首页 > 编程语言 >Python判断字符串是否为空

Python判断字符串是否为空

时间:2023-02-03 00:55:22浏览次数:41  
标签:为空 Python python 空格 print str 字符串

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

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

标签:为空,Python,python,空格,print,str,字符串
From: https://www.cnblogs.com/sherlock-V/p/17087856.html

相关文章

  • Python fir 单线程下载脚本
    importrequests,os,timeimporturllib3urllib3.disable_warnings(urllib3.exceptions.InsecureRequestWarning)frompathlibimportPathfromtqdmimporttqdmfrom......
  • Python 高级编程之面向对象(一)
    目录一、概述二、面向对象的概念介绍1)类定义2)类属性3)类方法4)构造方法(init)三、面向对象封装、继承和多态1)封装2)继承3)子类重新方法和super()调用父类方法4)多态一、概述科班......
  • Python fir 下载脚本兼容iOS
    思路方法一:类似Android处理,用接口下载文件后,直接通过数据线进行安装方法二:通过服务端返回下载URL,iOS用快捷指令处理 最后选取的第二种方法。对之前脚本做如下修改:1......
  • python内置函数map
    map()函数介绍map()是python的一个内置函数,其作用是返回一个迭代器,该迭代器将function函数应用于iterable的每个项,并产生结果。map函数的语法:map(function,iterable,......
  • Jupyter IPython Anaconda
    IPythonNotebook改名叫JupyterNotebook了。ref:​​​windows中jupyter(原ipythonnotebook)中文件工作路径的设置​​Anaconda是专注于数据分析的Python发行版本,包含......
  • 用Python连接IBM量子API实现的量子算法——Deutsch-Jozsa算法
    Prefix我是看这篇文章:​​不再神秘的量子计算,用Python就能实现(视频+代码),希望能run上面的代码。量子代码地址:​​https://github.com/llSourcell/quantum_computing​​结果......
  • Python @ 装饰器
    一、简单例子先看下面一个简单的例子程序t1清单deffunA(fn):print('A')fn()#fn实际就是funB(函数对象)@funAdeffunB():print('B')#if__name__=="__main......
  • python2 与python3 区别
    写文件下面这段代码,在python2中可以正常运行。但是python3会报错,因为是用w方式打开(而不是wb),则二进制数据不能写入该文件(会报错)。withopen("a.txt",'w')aspfile:pfil......
  • C语言学习 字符串
    如果文件编码是GBK,那么他会编译成GBK编码,存储起来。   内存里面这么存储。中文是GBK编码存储,而数字和英文,是以unicode编码存储   GBK编码查询   宽......
  • 看到发狂的moveElement函数中的字符串拼接
    vartimer=setTimeout("moveEleme('"+eleID+"',"+leftMar+","+topMar+","+interval+")",interval)这是我调用时候传的参数:moveEleme("test",20......