Open In App

Test if String Contains Alphabets and Spaces – Python

Last Updated : 17 Jan, 2025
Summarize
Comments
Improve
Suggest changes
Like Article
Like
Share
Report
News Follow

We are given a string and the task is to determine whether it contains only alphabets and spaces, this is often required when working with datasets where the string must not include any digits, punctuation or special characters.

Using all() isspace() and isalpha()

This method iterates through each character in the string, checking whether all characters are either alphabets (isalpha()) or spaces (isspace()) and the all() function ensures the condition holds true for the entire string.

s = "geeksforgeeks is best for geeks"

# Check if all characters are alphabets or spaces
res = s != '' and all(c.isalpha() or c.isspace() for c in s)

print("Does the string contain only alphabets and spaces:", res)

Output
Does the string contain only alphabets and spaces: True

Explanation:

  • all() function ensures that every character in the string is either an alphabet or a space
  • chr.isalpha() or chr.isspace() checks whether each character is alphabetic or a space

Using Regular Expressions 

In this method we are using re.match() to match the string against a regular expression that allows only alphabets and spaces. ( regex pattern [a-zA-Z\s]+$ ensures that only alphabetic characters and spaces are allowed in the string.)

import re

s = 'geeksforgeeks is best for geeks'

# Test if String contains Alphabets and Spaces
res = bool(re.match('[a-zA-Z\s]+$', s))

print("Does String contain only space and alphabets : " + str(res))

Output
Does String contain only space and alphabets : True

Explanation: re.match() function attempts to match the pattern at the start of the string and returns a boolean indicating whether the entire string contains the pattern or not.

Using operator.countOf() Method

This method uses the operator.countOf() function to check whether each character in the string is either an alphabet or a space.

import operator as op

s = "geeksforgeeks is 3best for geeks"  # String to check
a = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"  # All alphabets
sp = " "  # Space character

# Check if all characters are alphabets or spaces
res = s != ' ' and all(op.countOf(a, c) > 0 or op.countOf(sp, c) > 0 for c in s)

print("Does the string contain only alphabets and spaces:", res)

Output
Does the string contain only alphabets and spaces: False

Explanation:

  • all(...) ensures every character is either in the allowed alphabets (a) or is a space (sp).
  • s != ' ' is used to make sure that the string is not empty before the check

Using filter() and a Lambda Function

In this method we use the filter() function combined with a lambda function to check if all characters in the string belong to the set of allowed characters (alphabets and spaces).

s = "geeksforgeeks is best for geeks" 

chars = set("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ ")  # Allowed characters

# Filter out characters not in the allowed set
res = ''.join(filter(lambda c: c in chars, s)) == s

print("Does the string contain only alphabets and spaces:", res)

Output
Does the string contain only alphabets and spaces: True

Explanation:

  • filter(lambda c: c in allowed_chars, s) filters only allowed characters from the string.
  • The result is compared with the original string to ensure no invalid characters are present.

Using reduce() Method and Lambda Function

In this method we utilize the reduce() function from the functools module to iteratively check whether all characters in the string are either alphabets or spaces.

from functools import reduce

s = "geeksfo1rgeeks is best for geeks"  

# Use reduce to verify all characters are alphabets or spaces
res = reduce(lambda x, y: x and (y.isalpha() or y.isspace()), s, True)

print("Does the string contain only alphabets and spaces:", res)

Output
Does the string contain only alphabets and spaces: False


Next Article
Practice Tags :

Similar Reads

three90RightbarBannerImg