Python String isalnum() Function
The String isalnum() method returns True if the string is not empty and all the characters are alphanumeric. Otherwise, it returns False.
note
A character is alphanumeric if it is either a letter [a-z],[A-Z] or a number [0-9].
Syntax
my_string.isalnum()
isalnum() Parameters
Python String isalnum() function does not take any parameters.
isalnum() Return Value
Python String isalnum() function returns:
Trueif ALL characters in the string are alphanumeric.Falseif AT LEAST ONE character is NOT alphanumeric.
Examples
Example 1: Check if a String is alphanumeric with isalnum()
The isalnum() method returns True if all characters are alphanumeric.
For example, let's check if all characters in the string abc123 are alphanumeric:
my_str = 'abc123'
result = my_str.isalnum()
print(result) # Output: True
output
True
Example 2: Check if a String with Special Characters is alphanumeric with isalnum()
The isalnum() method returns False if at least one character is not alphanumeric.
my_str = 'abc@123'
result = my_str.isalnum()
print(result) # Output: False
output
False
Example 3: Check if an empty String is alphanumeric with isalnum()
The isalnum() method returns False if the string is empty.
my_str = ''
result = my_str.isalnum()
print(result) # Output: False
output
False