Skip to content
Related Articles

Related Articles

Improve Article
Save Article
Like Article

Python | Test if String contains Alphabets and Spaces

  • Last Updated : 17 Dec, 2021

Sometimes, while testing of credibility of string being a part of containing just alphabets, an exception of spaces has to be mentioned explicitly and becomes a problem. This can occur in domains that deal with data. Lets discuss certain ways in which this task can be performed.

Method #1 : Using all() + isspace() + isalpha() 
This is one of the way in which this task can be performed. In this, we compare the string for all elements being alphabets or space only.

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))
Output : 
The original string is : geeksforgeeks is best for geeks
Does String contain only space and alphabets : True

 

Method #1 : Using regex 
This problem can also be solved by employing regex to include only space and alphabets in a string.

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))
Output : 
The original string is : geeksforgeeks is best for geeks
Does String contain only space and alphabets : True

 


My Personal Notes arrow_drop_up
Recommended Articles
Page :

Start Your Coding Journey Now!