如何在Python中檢查文字是否“為空”(包含空格、製表符和換行符)?


在本文中,我們將重點介紹如何在Python中檢查文字是否為空(包含空格、製表符和換行符)。

第一種方法是使用內建字串庫的isspace()方法。isspace()方法用於確定字串是否只包含空格或包含其他字元。

如果給定的字串僅由空格組成,則此方法返回true;否則,返回false。即使字串包含't'和'n'之類的字元,該方法也會返回true。

示例

在下面的程式中,我們取三個不同的字串,並使用isspace()方法找出它們是否只包含空格

str1 = " "
str2 = "         DAS"
str3 = "\n\t"

print("Checking whether the given string'",str1,"'contains only spaces")
print(str1.isspace())

print("Checking whether the given string'",str2,"'contains only spaces")
print(str2.isspace())

print("Checking whether the given string'",str3,"'contains only spaces")
print(str3.isspace())

輸出

上面示例的輸出如下所示

Checking whether the given string' 'contains only spaces
True
Checking whether the given string' DAS 'contains only spaces
False
Checking whether the given string'
      'contains only spaces
True

使用正則表示式

第二種方法涉及使用正則表示式。re庫的search方法使用正則表示式“^\s*$”,如果字串只包含空格則返回true,如果字串包含任何其他字元則返回false。

示例1

在下面的程式中,我們取三個不同的字串,並使用正則表示式“^\s*$”和re庫的search方法找出它們是否只包含空格

import re
str1 = " "
str2 = " DAS"
str3 = "\n\t"

print("Checking whether the given string'",str1,"'contains only spaces")
if not str1 or re.search("^\s*$", str1):
   print('true')
else:
   print('false')
   
print("Checking whether the given string'",str2,"'contains only spaces")
if not str2 or re.search("^\s*$", str2):
   print('true')
else:
   print('false')
   
print("Checking whether the given string'",str3,"'contains only spaces")
if not str3 or re.search("^\s*$", str3):
   print('true')
else:
   print('false')

輸出

上面示例的輸出如下所示

Checking whether the given string' 'contains only spaces
true
Checking whether the given string' DAS 'contains only spaces
false
Checking whether the given string'
            'contains only spaces
true

示例2

為了只匹配空白字元,我們也可以使用正則表示式元字元\s呼叫re.match(regex, string),如下所示:“^\s*$

import re
print(bool(re.match('^\s+$', ' abc')))
print(bool(re.match('^\s+$', ' ')))

輸出

以下是上述程式碼的輸出

False
True

使用len()函式

第三種方法是使用內建的len()函式。必須確定給定字串的長度。如果字串的長度為0,則表示字串為空或僅包含空白字元;否則,字串包含其他字元。

示例

在下面的示例中,我們使用len()方法檢查給定的字串是否為空。

str1 = " "
str2 = "           DAS"
str3 = ""

print("Checking whether the given string'",str1,"'is empty")
if len(str1) == 0:
   print('true')
else:
   print('false')
   
print("Checking whether the given string'",str2,"'is empty")
if len(str2) == 0:
   print('true')
else:
   print('false')
   
print("Checking whether the given string'",str3,"'is empty")
if len(str3) == 0:
   print('true')
else:
   print('false')

輸出

上面示例的輸出如下所示

Checking whether the given string' 'is empty
false
Checking whether the given string' DAS 'is empty
false
Checking whether the given string' 'is empty
true

使用strip()函式

第四種方法是使用內建的strip()函式。strip()函式刪除字串中所有不必要的空格。要確定字串是否只包含空格,請將修剪後的字串與原始字串進行比較;如果它們相同,則字串只包含空格或為空。

示例

在下面的程式中,我們使用strip()方法並將其與實際字串進行比較,來檢查給定的字串是否只包含空格。

import re
str1 = " "
str2 = " DAS"
str3 = ""

print("Checking whether the given string'",str1,"'contains only spaces")
if str1 and str1.strip():
   print('false')
else:
   print('true')
   
print("Checking whether the given string'",str2,"'contains only spaces")
if str2 and str2.strip():
   print('false')
else:
   print('true')
   
print("Checking whether the given string'",str3,"'contains only spaces")
if str3 and str3.strip():
   print('false')
else:
   print('true')

輸出

上面示例的輸出如下所示

Checking whether the given string' 'contains only spaces
true
Checking whether the given string' DAS 'contains only spaces
false
Checking whether the given string' 'contains only spaces
true

更新於:2022年12月7日

4K+ 瀏覽量

開啟你的職業生涯

透過完成課程獲得認證

開始學習
廣告