python判断字符串是否为空和null

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

一、使用字符串长度判断spa

len(s) ==0  则字符串为空code

#!/user/local/python/bin/python # coding=utf-8
test1 = ''
if len(test1) == 0: print '字符串TEST1为空串'
else: print '字符串TEST1不是空串,TEST1:' + test1

二、isspace判断是否字符串所有是空格blog

Python isspace() 方法检测字符串是否只由空格组成。ip

#!/user/local/python/bin/python # coding=utf-8
 str = "       "; print str.isspace(); str = "This is string example....wow!!!"; print str.isspace(); True False

三、字符串去空格及去指定字符utf-8

去两边空格:str.strip()字符串

去左空格:str.lstrip()string

去右空格:str.rstrip()class

#!/user/local/python/bin/python # coding=utf-8
 str = "       "; print str.strip(); str = "This is string example....wow!!! "; print str.strip();