Python Function Reference

str.isalnum

Check that the string is not empty and contains only letters and digits.

Python 3.13
str.isalnum()

Return value

True or False; False for an empty string.

Examples

Letters and digits

Python 3.13
print('abc123'.isalnum())
True

An underscore does not match, Cyrillic does

Python 3.13
print('user_1'.isalnum(), 'Привет2'.isalnum())
False True

Details

Letters of any alphabet and digits match, while spaces, hyphens and underscores do not. To check a variable name, use str.isidentifier.

See also