The isalnum() function in python helps to check whether all the characters of a string are alphanumeric. The alphanumeric means it may be an alphabet (A-Z) or a number(0-9). The function returns true if all the characters are alphanumeric otherwise returns false.
string.isalnum()
The isalnum() doesn't take any parameters. This function does not allow any special chars( ()!#%&?) even spaces.
If the specified string is empty, then isalnum() returns False. It will return True even the string is full of digits.
Input | Return Value |
---|---|
all characters are alphanumeric | True |
all characters are not alphanumeric | False |
string = "A123testing"
print(string.isalnum())
# contains whitespace
string = "A123 testing"
print(string.isalnum())
string = "Atesting"
print(string.isalnum())
string = "123"
print(string.isalnum())
string = "123*&"
print(string.isalnum())
Output:
True False True True False
string = "Angel001"
if string.isalnum() == True:
print("All characters of string are alphanumeric.")
else:
print("All characters are not alphanumeric.")
Output:
All characters of string are alphanumeric.