Python String isalnum() Method

Determines whether the string contains alphanumeric characters

Usage

The isalnum() method returns TRUE if the string is nonempty and all characters in it are alphanumeric. Otherwise, it returns FALSE.

A character is alphanumeric if it is either a letter [a-z],[A-Z] or a number[0-9].

Syntax

string.isalnum()

Basic Example

# Check if all characters in the string are alphanumeric
S = 'abc123'
x = S.isalnum()
print(x)
# Prints True

isalnum() on String with Special Character

The isalnum() method returns FALSE if at least one character is not alphanumeric.

S = 'abc-123'
x = S.isalnum()
print(x)
# Prints False

S = '*abc123?'
x = S.isalnum()
print(x)
# Prints False

# even a space
S = 'abc 123'
x = S.isalnum()
print(x)
# Prints False

isalnum() on Empty String

The isalnum() method returns FALSE if the string is empty.

S = ''
x = S.isalnum()
print(x)
# Prints False