Python 如何检查字符串是否只包含空格字母

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

Camera课程

Python教程

Java教程

Web教程

数据库教程

图形图像教程

办公软件教程

Linux教程

计算机教程

大数据教程

开发工具教程