當前位置:首頁 » 編程語言 » python字元串為空判斷

python字元串為空判斷

發布時間: 2022-09-15 10:47:46

A. python字元串相等if,python判斷字元串是否為空用什麼方法

reaCode=None
if(reaCode==None):
print"isnull"
else:
print"not!"

if(reaCodeisNone):
print"isnull"
else:
print"not!"

reaCode=''
if(reaCodeis''):
print"isnullstring"
else:
print"not!"

reaCode=''
if(reaCode==''):
print"isnullstring"
else:
print"not!"

if(reaCode==None):
print"isnull"
else:
print"not!"

if(reaCodeisNone):
print"isnull"
else:
print"not!"

None是一個空的對象,代表什麼都沒有。
而'',是一個字元串對象,代表一個空的字元串。

B. Python中如何判斷一個字元串中有幾個空格

#!/usr/bin/envpython
#coding=utf-8

"""
Python中如何判斷一個字元串中有幾個空格
http://..com/question/138854675106454205.html
"""

from__future__import(print_function,unicode_literals)

text='2014.112016.03xxx有限公司(1年4個月)'

currentCharIsSpace=False
count=0
forcintext:
ifcurrentCharIsSpace:
ifc.isspace():
count+=1
else:
currentCharIsSpace=False
print("%s個空格"%(count,))
else:
ifc.isspace():
count=1
currentCharIsSpace=True
else:
print(c,end="")

ifcurrentCharIsSpace:
currentCharIsSpace=False
print("%s個空格"%(count,))

運行結果

2014.112個空格
016.032個空格
xx有限公司2個空格
1年4個月)

C. Python中None和'空字元串的區別有哪些

  1. None是值,pass是空語句,

    a=10
    if a>8:
    pass
    print a

    這個例子中a>8後什麼也補執行,相當於是個空操作.

    至於None就是不存在的意思,或者在判斷語句中就是「false"的意思。

  2. 兩者區別:
    None是一個空的對象,代表什麼都沒有。
    而是一個字元串對象,代表一個空的字元串。
    只有字元串才能和字元串相互連接。

    D. python 怎麼判斷文件的空行

    是空字元串還是空?
    空字元串也是有內容的,只是沒東西
    但為空,是指什麼都沒有
    先判斷是否為空
    if
    not
    str:
    print
    '空對象「
    if
    not
    len(str.strip())
    ##空字元串
    print
    '空字元串'
    如果不考慮是哪一種,可以結合來判斷
    if
    not
    str
    or
    not
    len(str.strip()):
    print
    '空』

    E. python如何判斷字元串為空

    a=''
    if not a:
    print "a是空的"
    輸出結果為:a是空的

    F. Python中s為空字元時,輸出s[0]會報錯,但是輸出s[0:]不會報錯

    這個問題不錯,首先s[0]其實和s[0:1]是一樣的獲取第一個元素的。所以空字元串會報錯。但是s[0:]這個是從0個元素開始到最後一個,如果空字元串就是s[0:0]所以不會報錯

    熱點內容
    安卓市場手機版從哪裡下載 發布:2025-05-15 20:17:28 瀏覽:813
    幼兒速演算法 發布:2025-05-15 20:15:08 瀏覽:86
    best把槍密碼多少 發布:2025-05-15 20:13:42 瀏覽:547
    android安裝程序 發布:2025-05-15 20:13:20 瀏覽:558
    c語言跳出死循環 發布:2025-05-15 20:06:04 瀏覽:823
    a19處理器相當於安卓哪個水平 發布:2025-05-15 20:05:29 瀏覽:638
    榮耀9i安卓強行關機按哪個鍵 發布:2025-05-15 20:00:32 瀏覽:750
    密碼鎖寫什麼最好 發布:2025-05-15 19:05:31 瀏覽:782
    5的源碼是 發布:2025-05-15 19:04:07 瀏覽:719
    c語言創建的源文件 發布:2025-05-15 18:54:08 瀏覽:611