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