當前位置:首頁 » 編程語言 » 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]所以不會報錯

    熱點內容
    java返回this 發布:2025-10-20 08:28:16 瀏覽:577
    製作腳本網站 發布:2025-10-20 08:17:34 瀏覽:870
    python中的init方法 發布:2025-10-20 08:17:33 瀏覽:566
    圖案密碼什麼意思 發布:2025-10-20 08:16:56 瀏覽:749
    怎麼清理微信視頻緩存 發布:2025-10-20 08:12:37 瀏覽:668
    c語言編譯器怎麼看執行過程 發布:2025-10-20 08:00:32 瀏覽:992
    郵箱如何填寫發信伺服器 發布:2025-10-20 07:45:27 瀏覽:239
    shell腳本入門案例 發布:2025-10-20 07:44:45 瀏覽:98
    怎麼上傳照片瀏覽上傳 發布:2025-10-20 07:44:03 瀏覽:790
    python股票數據獲取 發布:2025-10-20 07:39:44 瀏覽:696