Python String isalnum()

In this tutorial, you will learn about the Python String isalnum() method with the help of examples.

The isalnum() method returns True if all characters in the string are alphanumeric (either alphabets or numbers). If not, it returns False.


# string contains either alphabet or number 
name1 = "Python3"

print(name1.isalnum()) #True
# string contains whitespace name2 = "Python 3"
print(name2.isalnum()) #False

isalnum() Syntax

The syntax of the isalnum() method is:


Here, the isalnum() method checks if all the characters of string are alphanumeric or not.

isalnum() Parameters

The isalnum() method doesn't take any parameters.

isalnum() Return Value

The isalnum() method returns:

  • True - if all characters in the string are alphanumeric
  • False - if at least one character is not alphanumeric

Example 1: Python isalnum()

# contains either numeric or alphabet
string1 = "M234onica"

print(string1.isalnum()) # True
# contains whitespace string2 = "M3onica Gell22er"
print(string2.isalnum()) # False
# contains non-alphanumeric character string3 = "@Monica!"
print(string3.isalnum()) # False



In the above example, we have used the isalnum() method with different strings to check if every character in the string is alphanumeric.

Here, string1 contains either alphabet or numerical values so the method returns True.

The method returns False for string2 and string3 because they contain non-alphanumeric characters i.e. whitespace, @, !.

Example 2: isalnum() in if..else Statement

text = "Python#Programming123"

# checks if all the characters are alphanumeric if text.isalnum() == True:
print("All characters of string are alphanumeric.") else: print("All characters are not alphanumeric.")


All characters are not alphanumeric.

Here, we have used the if..else statement to check if all the characters in text are alphanumeric or not.

Since "Python#Programming123" contains # which is neither an alphabet nor a number, text.isalnum() holds False.

So the program executes the else statement.

Recommended readings:

Did you find this article helpful?