如何在 Python 中檢查字串是否只包含空格字元?


字串是一組字元,可以表示單個單詞或完整的句子。在 Python 中,無需使用資料型別說明符顯式宣告變數,可以直接將其賦值給文字。因此,與 Java 等其他技術相比,使用 Python 字串非常容易。

為了操作和訪問字串,Python 包含許多內建函式和方法。字串是 String 類的物件,它有多個方法,因為 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'", '\n\t', "'contains only spaces")
True

使用正則表示式

您還可以使用正則表示式檢查給定字串是否為空格。正則表示式“^\s*$”用於 re 庫的 search 方法,如果字串只包含空格,則返回 true,如果字串包含任何其他字元,則返回 false。

示例

在下面給出的程式中,我們正在獲取 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
false
("Checking whether the given string'", '\n\t', "'contains only spaces")
true

使用 len() 函式

Python 中的 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() 方法檢查給定字串是否只包含空格,並將其與實際字串進行比較。

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
true

更新時間: 2022年10月19日

12K+ 次瀏覽

開啟你的 職業生涯

透過完成課程獲得認證

開始學習
廣告