Check if a character is an alphabet, digit, or a whitespace in Python

Hello friends, in this tutorial I will how to check if a character is an alphabet, digit, or a whitespace in Python.

Check if a character is an alphabet, digit, or a whitespace

This tutorial will cover methods to check if a character is :

  • an alphabet.
  • a digit.
  • whitespace.
  • either an alphabet or a digit.

1 . To check if a character is an alphabet

I have used the isalpha() function, which returns a boolean value. It returns True if the character is an alphabet and False otherwise. In the example I have defined two string variables, sample_Str1 and sample_Str2, and initialised them as ‘C’ and ‘1’ respectively. To check if the character is an alphabet or not, I have attributed the isalpha() function. This returns a boolean value which is stored in two temporary variables, result1 and result2 respectively.

Code :

sample_Str1 = 'C'
sample_Str2 = '1'
result1 = sample_Str1.isalpha()
print(result1)
result2 = sample_Str2.isalpha()
print(result2)

Output :

True
False

2 . To check if character is a digit

I have the used the same example as latter. However I have used the isdigit() function instead of isalpha. Now when I print the variables, result1 and result2 it gives an output as False and True respectively. The output is such because, ‘C’ is an alphabet, hence the function returns False and as ‘1’ is a digit, it results into True.

Code : 

sample_Str1 = 'C'
sample_Str2 = '1'
result1 = sample_Str1.isdigit()
print(result1)
result2 = sample_Str2.isdigit()
print(result2)

Output :

False
True

3 . To check if character is a whitespace

isspace() checks if the character in question is a whitespace or not. If yes, it returns True else False. I have taken string variable, sample_Str which contains a four-character word. Using a for loop, I’ve iterated over the string, and for each iterated character I’ve attributed it with the isspace() function and print its output.

Code :

sample_Str = 'Co de'
for i in range(0, len(sample_Str)):
    result = sample_Str[i].isspace()
    print(result)

Output :

False
False
True
False
False

4 . Check if the character is either an alphabet or digit

To check if the character is an alphabet or digit, I have used the isalnum() function. I have taken the seven-character string value and stored it in temporary variable, sample_Str. Using a for loop I’ve iterated over the string. For each iterated character I’ve checked if it is either an alphabet or digit by attributing the isalpha() function to the character. The function will return True if the character is either an alphabet or number and False otherwise.

Code :

sample_Str = 'Co1 de2'
for i in range(0, len(sample_Str)):
    result = sample_Str[i].isalnum()
    print(result)

The function returns False, when it encounters a whitespace while iterating over the characters.

Output :

True
True
True
False
True
True
True

Thus you can know how to check if the character is an alphabet, digit, or whitespace in Python.

Leave a Reply

Your email address will not be published. Required fields are marked *