Python Glossary Русский

all Python 3Strings (str)function

str.isalnum

character checks

True if the string is non-empty and all characters are alphanumeric.

Syntax

s.isalnum()

The test is Unicode-wide, not [A-Za-z0-9]: Cyrillic passes, and so do oddities like the Roman numeral Ⅻ or the fraction ½, even though int() would reject them. Pair it with isascii(), or use a regex, when you really mean plain letters and digits. Underscore is not alphanumeric, so for a variable-name check use isidentifier() instead.

Examples

print('abc123'.isalnum())  # → True
print('a b'.isalnum())     # → False
print(''.isalnum())        # → False

See also

Open in the full glossarydocs.python.orgReport an inaccuracy