GeeksforGeeks App
Open App
Browser
Continue

# Python | Test if String contains Alphabets and Spaces

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```

Time Complexity: O(n)

Auxiliary Space: O(1)

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```

The Time and Space Complexity for all the methods are the same:

Time Complexity: O(n)

Auxiliary Space: O(n)

Method #3: Using operator.countOf() method

## Python3

 `# Python3 code to demonstrate working of``# Test if String contains Alphabets and Spaces``import` `operator as op` `# initializing string``test_str ``=` `'geeksforgeeks is best for geeks'``# create a string of allowed characters``allowedAlphabets ``=` `"abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"``allowedSpaces ``=` `' '``# printing original string``print``(``"The original string is : "` `+` `test_str)` `# Test if String contains Alphabets and Spaces``res ``=` `test_str !``=` `'' ``and` `all``(op.countOf(allowedAlphabets, ``chr``) > ``0` `or` `op.countOf(``    ``allowedSpaces, ``chr``) > ``0` `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```

Time Complexity: O(n)

Auxiliary Space: O(n)

Method #4:  Here’s an approach using a string method isalpha() and an in-built function any():

## Python3

 `# Python3 code to demonstrate working of``# Test if String contains Alphabets and Spaces` `# initializing string``test_str ``=` `'geeksfo1rgeeks is best for geeks'` `# printing original string``print``(``"The original string is : "` `+` `test_str)` `# Test if String contains Alphabets and Spaces``res ``=` `not` `any``(c ``for` `c ``in` `test_str ``if` `not` `c.isalpha() ``and` `not` `c.isspace())` `# printing result``print``(``"Does String contain only space and alphabets : "` `+` `str``(res))`

Output

```The original string is : geeksfo1rgeeks is best for geeks
Does String contain only space and alphabets : False```

Time Complexity: O(n)

Auxiliary Space: O(n)

Method #5: Using filter() and lambda function:

## Python3

 `def` `check_alphabets_and_spaces(s):``    ``allowed_chars ``=` `set``(``'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ '``)``    ``filtered_string ``=` `''.join(``filter``(``lambda` `c: c ``in` `allowed_chars, s))``    ``return` `filtered_string ``=``=` `s``test_string ``=` `"geeksforgeeks is best for geeks"``print``(``"The original string is:"``, test_string)``result ``=` `check_alphabets_and_spaces(test_string)``if` `result:``    ``print``(``"True"``)``else``:``    ``print``(``"False"``)` `#This code is contributed by Jyothi pinjala.`

Output

```The original string is: geeksforgeeks is best for geeks
True```

Time Complexity: O(n)

Auxiliary Space: O(n)

Method #6: Using reduce() method and lambda function:

Algorithm :

1. Import the reduce() method from the functools module.
2. Define the input string to be tested.
3. Use the reduce() method to iterate over each character in the input string, starting with an initial value of True.
4. Define a lambda function that takes two arguments: x and y.
5. For each character y in the input string, check if it is either an alphabet or a space using the isalpha() and isspace() methods.
6. If y is an alphabet or space, return x (which is initially True) and continue to the next character in the string.
7. If y is not an alphabet or space, return False.
8. The reduce() method will apply the lambda function to each character in the input string, and return False immediately if any character fails the test. If all characters pass the test, the lambda function will return True.
9. The result of the reduce() method is stored in the res variable.
10. Print the value of res.

## Python3

 `from` `functools ``import` `reduce``# initializing string``test_str ``=` `'geeksfo1rgeeks is best for geeks'``# printing original string``print``(``"The original string is : "` `+` `test_str)``res ``=` `reduce``(``lambda` `x, y: x ``and` `(y.isalpha() ``or` `y.isspace()), test_str, ``True``)``# printing result``print``(``"Does String contain only space and alphabets : "` `+` `str``(res))``#This code is contributed by Rayudu.`

Output

```The original string is : geeksfo1rgeeks is best for geeks
Does String contain only space and alphabets : False```

The time complexity : O(n), where n is the length of the input string.
This is because the reduce() function applies the lambda function to each character of the string exactly once, resulting in a linear time complexity

The auxiliary space : O(1), because the code only uses a constant amount of memory to store the input string and a boolean value. The reduce() function does not create a new list, but rather iterates over the input string one character at a time, and applies the lambda function to each character in turn.

My Personal Notes arrow_drop_up