📜  Python程序检查字符串是否为空

📅  最后修改于: 2022-05-13 01:55:10.345000             🧑  作者: Mango

Python程序检查字符串是否为空

Python字符串是不可变的,因此在谈论它的操作时会有更复杂的处理。请注意,带有空格的字符串实际上是一个空字符串,但大小不为零。本文还讨论了该问题及其解决方案。
让我们看看检查字符串是否为空的不同方法。
方法 #1:使用 len()
使用 len() 是检查零长度字符串的最通用方法。即使它忽略了一个事实,即只有空格的字符串实际上也应该被视为空字符串,即使它的非零也是如此。

Python3
# Python3 code to demonstrate
# to check if string is empty
# using len()
 
# initializing string
test_str1 = ""
test_str2 = "  "
 
# checking if string is empty
print ("The zero length string without spaces is empty ? : ", end = "")
if(len(test_str1) == 0):
    print ("Yes")
else :
    print ("No")
 
# prints No
print ("The zero length string with just spaces is empty ? : ", end = "")
if(len(test_str2) == 0):
    print ("Yes")
else :
    print ("No")


Python3
# Python3 code to demonstrate
# to check if string is empty
# using not
 
# initializing string
test_str1 = ""
test_str2 = "  "
 
# checking if string is empty
print ("The zero length string without spaces is empty ? : ", end = "")
if(not test_str1):
    print ("Yes")
else :
    print ("No")
 
# prints No
print ("The zero length string with just spaces is empty ? : ", end = "")
if(not test_str2):
    print ("Yes")
else :
    print ("No")


Python3
# Python3 code to demonstrate
# to check if string is empty
# using not + strip()
 
# initializing string
test_str1 = ""
test_str2 = "  "
 
# checking if string is empty
print ("The zero length string without spaces is empty ? : ", end = "")
if(not (test_str1 and test_str1.strip())):
    print ("Yes")
else :
    print ("No")
 
# prints Yes
print ("The zero length string with just spaces is empty ? : ", end = "")
if(not(test_str2 and test_str2.strip())):
    print ("Yes")
else :
    print ("No")


Python3
# Python 3 code to demonstrate
# to check if string is empty
# using not + isspace()
 
# initializing string
test_str1 = ""
test_str2 = "  "
 
# checking if string is empty
print ("The zero length string without spaces is empty ? : ", end = "")
if(not (test_str1 and not test_str1.isspace())):
    print ("Yes")
else :
    print ("No")
 
# prints Yes
print ("The zero length string with just spaces is empty ? : ", end = "")
if(not (test_str2 and not test_str2.isspace())):
    print ("Yes")
else :
    print ("No")


输出 :

The zero length string without spaces is empty ? : Yes
The zero length string with just spaces is empty ? : No


方法#2:不使用
not运算符也可以执行类似于 len() 的任务,并检查长度为 0 的字符串,但与上面相同,它认为只有空格的字符串也是非空的,这实际上不应该是真的。

Python3

# Python3 code to demonstrate
# to check if string is empty
# using not
 
# initializing string
test_str1 = ""
test_str2 = "  "
 
# checking if string is empty
print ("The zero length string without spaces is empty ? : ", end = "")
if(not test_str1):
    print ("Yes")
else :
    print ("No")
 
# prints No
print ("The zero length string with just spaces is empty ? : ", end = "")
if(not test_str2):
    print ("Yes")
else :
    print ("No")

输出 :

The zero length string without spaces is empty ? : Yes
The zero length string with just spaces is empty ? : No


方法 #3:使用 not + str.strip()
使用strip()可以解决空+零长度字符串的问题,strip()遇到空格返回true,因此检查它可以解决检查纯空字符串的问题。

Python3

# Python3 code to demonstrate
# to check if string is empty
# using not + strip()
 
# initializing string
test_str1 = ""
test_str2 = "  "
 
# checking if string is empty
print ("The zero length string without spaces is empty ? : ", end = "")
if(not (test_str1 and test_str1.strip())):
    print ("Yes")
else :
    print ("No")
 
# prints Yes
print ("The zero length string with just spaces is empty ? : ", end = "")
if(not(test_str2 and test_str2.strip())):
    print ("Yes")
else :
    print ("No")

输出 :

The zero length string without spaces is empty ? : Yes
The zero length string with just spaces is empty ? : Yes


方法#4:使用 not + str.isspace
工作方式与上述方法类似,并检查字符串中的空格。这种方法更有效,因为 strip() 需要执行带计算负载的剥离操作,如果没有的话。的空间是很好的数字。

Python3

# Python 3 code to demonstrate
# to check if string is empty
# using not + isspace()
 
# initializing string
test_str1 = ""
test_str2 = "  "
 
# checking if string is empty
print ("The zero length string without spaces is empty ? : ", end = "")
if(not (test_str1 and not test_str1.isspace())):
    print ("Yes")
else :
    print ("No")
 
# prints Yes
print ("The zero length string with just spaces is empty ? : ", end = "")
if(not (test_str2 and not test_str2.isspace())):
    print ("Yes")
else :
    print ("No")

输出 :

The zero length string without spaces is empty ? : Yes
The zero length string with just spaces is empty ? : Yes