當前位置:首頁 » 編程語言 » 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-07-03 03:22:49 瀏覽:22
    中國電信加密通信業務 發布:2025-07-03 03:06:00 瀏覽:520
    腳本家的台詞 發布:2025-07-03 03:05:50 瀏覽:708
    arcgisforpython 發布:2025-07-03 03:05:46 瀏覽:898
    期計演算法 發布:2025-07-03 02:56:53 瀏覽:404
    不同域訪問 發布:2025-07-03 02:49:05 瀏覽:818
    多槽編程 發布:2025-07-03 02:47:42 瀏覽:919
    sql2008錯誤233 發布:2025-07-03 02:28:52 瀏覽:168
    創建資料庫語句mysql 發布:2025-07-03 02:14:34 瀏覽:146
    python量化投資 發布:2025-07-03 02:05:11 瀏覽:804