Python|测试字符串是否包含字母和空格
有时,虽然测试字符串的可信度是仅包含字母的一部分,但必须明确提及空格的例外并成为一个问题。这可能发生在处理数据的域中。让我们讨论可以执行此任务的某些方式。
方法#1:使用 all() + isspace() + isalpha()
这是可以执行此任务的方式之一。在此,我们比较所有元素的字符串,仅是字母或空格。
Python3
# Python3 code to demonstrate working of
# Test if String contains Alphabets and Spaces
# Using isspace() + isalpha() + all()
import re
# initializing string
test_str = 'geeksforgeeks is best for geeks'
# printing original string
print("The original string is : " + test_str)
# Test if String contains Alphabets and Spaces
# Using isspace() + isalpha() + all()
res = test_str != '' and all(chr.isalpha() or chr.isspace() for chr in test_str)
# printing result
print("Does String contain only space and alphabets : " + str(res))
Python3
# Python3 code to demonstrate working of
# Test if String contains Alphabets and Spaces
# Using regex
import re
# initializing string
test_str = 'geeksforgeeks is best for geeks'
# printing original string
print("The original string is : " + test_str)
# Test if String contains Alphabets and Spaces
# Using regex
res = bool(re.match('[a-zA-Z\s]+$', test_str))
# printing result
print("Does String contain only space and alphabets : " + str(res))
输出 :
The original string is : geeksforgeeks is best for geeks
Does String contain only space and alphabets : True
方法#1:使用正则表达式
这个问题也可以通过使用正则表达式在字符串中只包含空格和字母来解决。
Python3
# Python3 code to demonstrate working of
# Test if String contains Alphabets and Spaces
# Using regex
import re
# initializing string
test_str = 'geeksforgeeks is best for geeks'
# printing original string
print("The original string is : " + test_str)
# Test if String contains Alphabets and Spaces
# Using regex
res = bool(re.match('[a-zA-Z\s]+$', test_str))
# printing result
print("Does String contain only space and alphabets : " + str(res))
输出 :
The original string is : geeksforgeeks is best for geeks
Does String contain only space and alphabets : True