python判斷字符串是否為空和null


1、使用字符串長度判斷

len(s==0)則字符串為空

test1 = ''
if len(test1) == 0:
    print('test1為空串')
else:
    print('test非空串,test='+test1)

2、isspace判斷字符串是否只由空格組成

>>> str=""
>>> print(str.isspace())
False
>>> str="   "
>>> print(str.isspace())
True
>>> str="a   "
>>> print(str.isspace())
False
>>>

3、字符串去空格及去指定字符

  • 去兩邊空格:str.strip()
  • 去左空格:str.lstrip()
  • 去右空格:str.rstrip()

4、python中沒有NULL,與之對應的是None

判斷字符串是否為空或者none

test1 = None
if test1:
    print('字符串不是空也不是None')
else:
    print('字符串是None或者空串')


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM