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

Example: Check if all characters in the string are alphanumeric

S = 'abc123'
x = S.isalnum()
print(x)    # True

isalnum() on String with Special Character

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

Example:

S = 'abc-123'
x = S.isalnum()
print(x)    # False
S = '*abc123?'
x = S.isalnum()
print(x)    # False

even a space

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

isalnum() on Empty String

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

Example:

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