如何在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