Tutorial Study Image

Python isalnum()

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() 
 

isalnum() Parameters:

The isalnum() doesn't take any parameters. This function does not allow any special chars( ()!#%&?) even spaces.

isalnum() Return Value

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

Examples of isalnum() method in Python

Example 1: Working of isalnum()in Python


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

Example 2: Working of isalnum() with condition checking


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.