如何在Python中检查字符串是否为空?

6,359 阅读4分钟

How to Check If String is Empty or Not in Python

Python中的字符串数据类型是不可改变的;这意味着一旦定义了字符串,就不能改变它。带有空格的字符串是一个空字符串,但其大小不为零。因此,当你用len()not 操作符检查空字符串时,它把空格算作字符串中的一个字符,因此它不会把带有空格的字符串算作空字符串。

如何在Python中检查空字符串

Python检查一个 字符串

  1. 使用not 操作符
  2. 使用**len()**函数
  3. 使用 not + string.strip()
  4. 使用not + string.isspace()

检查空字符串的not操作符

Python中的not操作符检查只有空格的字符串是否为非空,这实际上不应该是

# app.py

strA = ""

# checking if string is empty
print("Check if the strA is empty : ", end="")
if(not strA):
    print("The string is empty")
else:
    print("No it is not empty")

输出

Check if the strA is empty : The string is empty

字符串是空的,需要注意的是,我们甚至没有在字符串中添加空格。这就是为什么它返回为空。另一方面,如果我们在字符串中放了一个空格,它就不算是空的,而not操作符 就会返回False。

# app.py

strA = " "

# checking if string with space is empty
print("Check if the strA with space is empty : ", end="")
if(not strA):
    print("The string is empty")
else:
    print("No it is not empty")

输出

Check if the strA with space is empty : No it is not empty

使用 len() 函数

要检查Python中的空字符串,可以使用len()函数;如果它返回0,意味着字符串是空的;否则就不是。

所以,如果字符串有东西,它将算作一个非空字符串;否则,它就是一个空字符串。

strA = ""

# checking if string with space is empty
print("Check if the string is empty : ", end="")
if(len(strA)):
    print("The string is not empty")
else:
    print("The string is empty")

输出

Check if the string is empty : The string is empty

在这里,如果条件因为返回0而变成了 ,那就意味着其他条件变成了真, 并且字符串是空的。

如果字符串包含空格,它就不算是一个空字符串。

strA = " "

# checking if string with space is empty
print("Check if the string is empty : ", end="")
if(len(strA)):
    print("The string is not empty")
else:
    print("The string is empty")

输出

Check if the string is empty : The string is not empty

字符串中的空格算作一个字符。这就是为什么当你检查是否为空时,它返回False。

使用 len() + string.strip()

要在Python中检查一个纯粹的空字符串,可以使用len()+string.strip()方法。string.strip()方法将字符串中的空白部分删除。因此,如果它包含任何空格,strip()函数会将其删除,并使用len()函数检查该字符串是否为空。

str = "   "

# checking if string with space is empty
print("Check if the string is empty : ", end="")
if(len(str.strip())):
    print("The string is not empty")
else:
    print("The string is empty")

输出

Check if the string is empty : The string is empty

在这里,无论你在字符串中添加多少空格,它都会剥离所有的空格,并检查字符串的长度,如果它返回0,这意味着字符串是空的;否则,就不是。

str = " KRUNAL  "

# checking if string with space is empty
print("Check if the string is empty : ", end="")
if(len(str.strip())):
    print("The string is not empty")
else:
    print("The string is empty")

输出

Check if the string is empty : The string is not empty

在这个例子中,你可以看到这个字符串不是空的,因为它有某些字符。所以len()方法返回字符串的长度,如果条件是返回True。

使用not + string.isspace()

string.isspace()函数检查字符串是否包含任何空格。如果字符串包含任何空格,那么它返回True。 否则,它将返回False。

我们使用stringnot string.isspace() 方法的组合来检查字符串是否为空,而不考虑空格。

这个方法比strip()方法更有效,因为它需要执行strip操作,如果字符串包含许多空格,则需要计算加载时间。

str = "  "

# checking if string with space is empty
print("Check if the string is empty : ", end="")
if(str and not str.isspace()):
    print("The string is not empty")
else:
    print("The string is empty")

输出

Check if the string is empty : The string is empty

在这里,我们用和运算符 检查isspace()函数的否定条件**。**

如果其中一个条件变成False, 那么由于**"和运算符 "** 如果条件返回False, 条件将被执行。

这是在Python中检查纯空字符串的较好方法。

本教程就到此为止。