The String isalnum() method returns True if the string is not empty and all the characters are alphanumeric. Otherwise, it returns False.
Syntax
my_string.isalnum()isalnum() Parameters
Python String isalnum() method does not take any parameters.
isalnum() Return Value
Python String isalnum() method 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: Trueoutput
TrueExample 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: Falseoutput
FalseExample 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: Falseoutput
False