如何在Python中检查一个字符串是否为空或空白

1,693 阅读12分钟

Python中的字符串是用来处理文本数据的。在对文本数据进行操作时,我们可能需要删除空字符串或空白处。当我们打印一个空字符串或空白处时,我们无法区分这两者的区别。在这篇文章中,我们将讨论在Python中检查一个字符串是否为空或空白的不同方法。这将有助于你区分空字符串和空白处。

在Python中使用等价运算符检查一个字符串是否为空或白空间

使用等价运算符检查一个字符串是否为空,我们可以将该字符串与另一个空字符串进行比较。如果结果是 "真",说明输入的字符串是空的。否则不是。

input_string=""
print("The input string is:",input_string)
if input_string=="":
    print("Input string is an empty string.")

输出

The input string is: 
Input string is an empty string.

要检查字符串是否只包含空白字符,可以将其与一个空字符串比较。如果结果为 "假",那么该字符串将包含空白字符。你可以在下面的例子中观察到这一点。

input_string=" "
print("The input string is:",input_string)
if input_string=="":
    print("Input string is an empty string.")
else:
    print("Input string is a whitespace character.")

输出

The input string is:  
Input string is a whitespace character.

在这里,如果输入的字符串中含有除空白以外的字符,也会返回False,如下图所示。

input_string="Aditya"
print("The input string is:",input_string)
if input_string=="":
    print("Input string is an empty string.")
else:
    print("Input string is a whitespace character.")

输出

The input string is: Aditya
Input string is a whitespace character.

在上面的例子中,你可以看到我们使用了字符串“Aditya” 。然而,程序告诉我们,这个字符串只包含空白处。因此,该程序在逻辑上是不正确的。因此,只有当你确定输入的字符串要么是空字符串,要么只包含空白字符时,你才能使用这种方法。

在Python中使用len()函数来检查一个字符串是空的还是空白的

len() 函数用于查找一个可迭代对象的长度,如列表、字符串、元组等。它将可迭代对象作为其输入参数,并返回可迭代对象的长度。

要使用Python中的 len() 函数检查一个字符串是否为空或空白,我们将使用以下步骤。

  • 首先,我们将使用len() 函数找到输入字符串的长度。我们将把这个长度存储在一个变量str_len
  • 现在,我们将检查输入字符串的长度是否为0。
  • 如果输入字符串的长度为0,我们将说该字符串是一个空字符串。否则,我们将说该字符串含有空白。

你可以在下面的例子中观察到这一点。

input_string=""
print("The input string is:",input_string)
str_len=len(input_string)
if str_len==0:
    print("Input string is an empty string.")
else:
    print("Input string is a whitespace character.")

输出。

The input string is: 
Input string is an empty string.

同样,如果输入的字符串中含有空白以外的字符,程序将给出错误的结果,如下图所示。

input_string = "Aditya"
print("The input string is:", input_string)
str_len = len(input_string)
if str_len == 0:
    print("Input string is an empty string.")
else:
    print("Input string is a whitespace character.")

输出

The input string is: Aditya
Input string is a whitespace character.

同样,你可以看到我们使用了字符串 “Aditya” 。然而,程序告诉我们,这个字符串只包含空白字符。因此,该程序在逻辑上是不正确的。因此,只有当你确定输入的字符串要么是空字符串,要么只包含空白字符时,你才能使用这种方法。

在 Python 中使用 not 操作符查找字符串是否为空或空白

我们也可以在Python中使用not操作符来检查一个字符串是否为空或空白。

在 Python 中,所有的可迭代对象,如字符串、列表和图元,当它们是空的时候,评估为 False。因此,一个空字符串的值是 False。

为了使用 not 操作符检查一个字符串是否为空或空白,我们将在输入的字符串上使用 not 操作符。如果该字符串是空的,那么该字符串将评估为False。然后,not运算符将把结果转换为True。

因此,如果输出为True,我们将说该字符串为空。否则,我们将说该字符串含有空白。你可以在下面的例子中观察到这一点。

input_string = ""
print("The input string is:", input_string)
if not input_string:
    print("Input string is an empty string.")
else:
    print("Input string is a whitespace character.")

输出

The input string is: 
Input string is an empty string.

同样,如果输入的字符串包含除空格以外的字符,程序将说该字符串只包含空格。因此,只有当你确定输入的字符串要么是空字符串,要么只包含空白字符时,你才能使用这种方法。

在Python中使用For循环来检查一个字符串是空的还是空白的

在Python中有六个空白字符,即空格“ ”, tab “\t” ,换行“\n” ,垂直tab”\v” ,回车“\r” ,以及feed “\f” 。我们可以使用这些空白字符的列表和一个for循环来检查一个字符串是否为空或空白。为此,我们将使用以下步骤。

  • 首先,我们将定义一个名为whitespaces 的列表来存储空白字符。
  • 然后,我们将定义一个变量isWhiteSpace 并将其初始化为 True。
  • 现在,我们将使用平等运算符检查输入字符串是否为空字符串。
  • 如果输入的字符串是一个空字符串,我们将打印出来。
  • 如果输入的字符串不是空的,我们将使用for循环遍历输入字符串的各个字符。
  • 在for循环中,我们将使用成员运算符和whitespaces 列表来检查当前字符是否是一个空白字符。
  • 如果当前字符不是空白字符,我们将给变量isWhiteSpace 赋值为False。然后,我们将使用 break语句跳出for循环。
  • 在for循环之外,如果isWhiteSpace 是True,我们将打印该字符串只由空白字符组成。
  • 否则,我们将打印出该字符串包含除空格以外的字符。

你可以在下面的例子中观察整个过程。

input_string = "    "
whitespaces = [" ", "\t", "\n", "\v", "\r", "\f"]
print("The input string is:", input_string)
isWhiteSpace = True

if input_string == "":
    print("Input string is an empty string.")
else:
    for character in input_string:
        if character in whitespaces:
            continue
        else:
            isWhiteSpace = False
            break
if isWhiteSpace:
    print("The string contains only whitespace characters.")
else:
    print("The string contains characters other than whitespaces.")

输出

The input string is: 
The string contains only whitespace characters.

在Python中使用列表理解来查找一个字符串是否为空或空白

列表理解被用来从一个现有的可迭代对象中创建一个列表。然而,我们可以使用all() 函数和列表理解来检查 Python 中的输入字符串是否为空或空白。

all() 函数将一个可迭代对象作为其输入参数。如果可迭代对象的所有元素的值都是 True,它就返回 True。否则,它返回 False。

要使用 Python 中的列表理解法检查一个字符串是否为空或空白,我们将使用以下步骤。

  • 首先,我们将定义一个名为whitespaces 的列表来存储空白字符。
  • 然后,我们将使用平等运算符检查输入的字符串是否为空字符串。
  • 如果输入的字符串是一个空字符串,我们将打印出来。
  • 如果输入的字符串不是空的,我们将使用列表理解法创建一个布尔值的列表。
  • 在列表理解中,如果输入字符串中的字符是一个空白字符,我们将在输出列表中包括True。否则,我们将在输出列表中包括False。
  • 在创建了布尔值的列表后,我们将把它传递给all() 函数。如果 all() 函数返回True,这意味着该字符串只包含空白字符。因此,我们将打印相同的内容。
  • 如果all()函数返回False,我们将打印出输入的字符串包含除空白字符以外的字符。

你可以在下面的例子中观察整个过程。

input_string = "    "
whitespaces = [" ", "\t", "\n", "\v", "\r", "\f"]
print("The input string is:", input_string)
if input_string == "":
    print("Input string is an empty string.")
else:
    myList = [True for character in input_string if character in whitespaces]
    output = all(myList)
    if output:
        print("The string contains only whitespace characters.")
    else:
        print("The string contains characters other than whitespaces.")

输出

The input string is: 
The string contains only whitespace characters.

在Python中使用strip()方法检查一个字符串是否为空或空白字符

strip() 方法用于从一个字符串中去除前导或尾部的空格。当invoked() 在一个字符串上时,从字符串中删除前导和尾部的空格。执行后,它返回修改后的字符串。

为了在Python中检查一个字符串是否为空或空白,我们将使用以下步骤。

  • 首先,我们将使用平等运算符检查字符串是否为空。
  • 如果字符串是空的,我们将打印出来。否则,我们将对该字符串调用strip() 方法。
  • 如果strip() 方法返回一个空字符串,我们可以说原始字符串只包含空白字符。因此,我们将打印如此。
  • 如果strip() 方法返回一个非空字符串,那么输入的字符串就包含了除空白字符以外的其他字符。因此,我们将打印出该字符串包含除空白字符以外的其他字符。

你可以在下面的例子中观察整个过程。

input_string = "   . "
print("The input string is:", input_string)
if input_string == "":
    print("Input string is an empty string.")
else:
    newStr = input_string.strip()
    if newStr == "":
        print("The string contains only whitespace characters.")
    else:
        print("The string contains characters other than whitespaces.")

输出

The input string is:    . 
The string contains characters other than whitespaces.

这种方法即使对含有非空白字符的字符串也很有效。因此,你可以在各种情况下使用这种方法。

在Python中使用isspace()方法检查一个字符串是否为空或白位

isspace() 方法是用来检查一个字符串是否只包含空白字符。当在一个字符串上调用时,如果该字符串只包含空白字符,isspace() 方法返回 True。否则,它将返回 "假"。

在Python中使用isspace() 方法来检查一个字符串是否为空或空白,我们将使用以下步骤。

  • 首先,我们将使用平等运算符检查字符串是否为空。
  • 如果字符串是空的,我们将打印出来。否则,我们将对该字符串调用isspace() 方法。
  • 如果isspace() 方法返回True,这意味着输入的字符串只包含空白字符。
  • 如果isspace() 方法返回False,则说明输入的字符串包含了除空白字符以外的其他字符。因此,我们将打印出该字符串包含空白字符以外的字符。

你可以在下面的例子中观察到这一点。

input_string = "  A "
print("The input string is:", input_string)
if input_string == "":
    print("Input string is an empty string.")
elif input_string.isspace():
    print("The string contains only whitespace characters.")
else:
    print("The string contains characters other than whitespaces.")

输出

The input string is:   A 
The string contains characters other than whitespaces.

同样,这种方法即使对含有非空白字符的字符串也能很好地工作。因此,你可以在各种情况下使用这种方法。

在Python中使用正则表达式检查一个字符串是否为空或白位

正则表达式被用来在Python中有效地处理字符串。我们也可以使用正则表达式来检查一个给定的字符串是否是空的或空白的。为此,我们将使用search() 函数。

search() 函数将一个字符串模式作为其第一个输入参数,将一个字符串作为其第二个输入参数。执行后,它返回一个匹配对象。search() 如果作为第二个输入参数的输入字符串中的子串与作为第一个输入参数的模式相匹配,则匹配对象不是无。如果该模式在字符串中不存在,那么匹配对象将是None。

要使用search() 函数检查一个给定的字符串是否为空或空白字符,我们将使用以下步骤。

  • 首先,我们将使用平等运算符检查字符串是否为空。
  • 如果该字符串是空的,我们将打印出来。否则,我们将使用search() 函数来检查该字符串是否只包含空白字符。
  • 由于我们需要检查字符串是否只包含空白字符,我们将检查字符串中是否有任何非空白字符。为此,我们将把模式 “\S” 作为第一个输入参数传给search() 函数。同时,我们将把输入的字符串作为第二个输入参数传给search() 函数。
  • 如果search() 函数返回None,这意味着字符串中没有非白色空格字符。因此,我们将打印出该字符串只包含空白字符。
  • 如果search() 函数返回一个匹配对象,我们将说该字符串包含除空白字符以外的字符。

你可以在下面的例子中观察到这一点。

from re import search

input_string = "   "
pattern = "\\S"
print("The input string is:", input_string)
if input_string == "":
    print("Input string is an empty string.")
else:
    match_object = search(pattern, input_string)
    if match_object is None:
        print("The string contains only whitespace characters.")
    else:
        print("The string contains characters other than whitespaces.")

输出

The input string is:    
The string contains only whitespace characters.

结论

在这篇文章中,我们讨论了在Python中检查一个字符串是否为空或空白的不同方法。在所有三种方法中,使用平等运算符、非运算符和len() 函数的方法在逻辑上是不正确的。只有当我们确定输入的字符串要么是空字符串,要么只由空白字符组成时,才可以使用它们。

使用strip() 方法、isspace() 方法和re.search() 函数的方法是稳健的。这些方法可以在所有情况下使用。因此,我建议你在Python中使用这些方法来检查一个给定的字符串是否为空或空白。