如何在 Python 中檢查文字是否“為空”(包含空格、製表符、換行符)?
在本文中,我們將重點介紹如何在 Python 中檢查文字是否為空(包含空格、製表符、換行符)。
第一種方法是使用內建字串庫的 isspace() 方法。isspace() 方法用於判斷字串是否僅包含空格或包含其他字元。
如果給定的字串僅由空格組成,則此方法返回 True;否則,返回 False。即使字串包含像 t 和 n 這樣的字元,該方法也會返回 True。
示例
在下面給出的程式中,我們獲取了 3 個不同的字串,並使用 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
在下面給出的程式中,我們獲取了 3 個不同的字串,並使用正則表示式“^\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
為了僅匹配空白字元,我們還可以使用 re.match(regex, string) 並使用正則表示式元字元 \s,如下所示:“^\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 後的字串與原始字串進行比較;如果它們相同,則字串僅包含空格或為空。
示例
在下面給出的程式中,我們使用 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
資料結構
網路
關係資料庫管理系統
作業系統
Java
iOS
HTML
CSS
Android
Python
C 程式設計
C++
C#
MongoDB
MySQL
Javascript
PHP