Python String isalnum() Method

Short answer
str.isalnum() returns True if the string is not empty and every character is a letter or a digit. Spaces, underscores and punctuation make it False.

isalnum() (alphanumeric) is the usual first test for user names, product codes and identifiers that may contain letters and digits but nothing else. It combines isalpha() and isdigit(): a character passes if either of them would accept it. Like those methods, it works on Unicode text, so accented letters pass too.

Quick facts
Syntaxtext.isalnum()
ParametersNone
ReturnsTrue if all characters are letters or digits and the string is not empty, else False
Empty stringFalse
Not allowedSpaces, _, -, . and other punctuation

What isalnum() considers alphanumeric

Letters and digits pass; everything else fails, including the underscore that is common in user names.

Python
for s in ["user42", "user_42", "user 42", "42", "abc", ""]:
    print(repr(s), s.isalnum())

Allowing an extra character such as underscore

To accept a few extra characters, remove them before testing. This keeps the rule explicit and easy to change.

Python
username = "asha_rao_7"
print(username.replace("_", "").isalnum())

For more complex rules (length limits, must start with a letter), a regular expression is clearer; see Python regex.

isalnum() vs isalpha() vs isdigit()

MethodAccepts`"abc123"``"abc"``"123"`
isalnum()Letters or digitsTrueTrueTrue
isalpha()Letters onlyFalseTrueFalse
isdigit()Digits onlyFalseFalseTrue
isidentifier()Valid Python nameTrueTrueFalse
isascii()Only ASCII charactersTrueTrueTrue

Examples you can run

Press Run, then edit the code and run it again.

Test a few strings

The basic behaviour for letters, digits, spaces and punctuation.

Test a few strings
for s in ["abc123", "abc-123", "ABC", "123"]:
    print(s, s.isalnum())
Expected outputabc123 True
abc-123 False
ABC True
123 True

Validate a username

A rule of letters and digits, with a length check as well.

Validate a username
username = input("Username: ")
if username.isalnum() and 4 <= len(username) <= 12:
    print("Accepted")
else:
    print("Use 4-12 letters or digits.")
Expected outputUsername: asha42
Accepted

Count alphanumeric characters

isalnum() works on single characters too, so it can filter or count.

Count alphanumeric characters
text = "Room 12-B, floor 3!"
print(sum(ch.isalnum() for ch in text))
print("".join(ch for ch in text if ch.isalnum()))
Expected output13
Room12Bfloor3

Strip punctuation from words

A common cleaning step before counting words.

Strip punctuation from words
words = ["hello,", "world!", "(python)"]
clean = ["".join(c for c in w if c.isalnum()) for w in words]
print(clean)
Expected output['hello', 'world', 'python']

Common mistakes

Expecting spaces to be allowed

✗ Wrong

print("room 12".isalnum())
Prints False because of the space.

✓ Right

print("room 12".replace(" ", "").isalnum())

Remove spaces first, or test each word separately.

Expecting the underscore to count

✗ Wrong

print("user_1".isalnum())
Prints False because `_` is punctuation.

✓ Right

print("user_1".replace("_", "").isalnum())

Remove the underscore before the test, or use a regular expression.

Assuming it checks for both letters and digits

✗ Wrong

print("abcdef".isalnum())  # thinking it needs a digit too
Prints True: letters alone are enough.

✓ Right

pw = "abcdef"
print(pw.isalnum() and any(c.isdigit() for c in pw))

To require both, add separate tests with any().

Python String isalnum() Method: questions people ask

What does isalnum() mean in Python?

It checks that a string is non-empty and contains only alphanumeric characters, meaning letters and digits.

Does isalnum() allow underscores or spaces?

No. Both are punctuation or whitespace, so a string containing them returns False.

How do I check a string has both letters and numbers?

Combine tests: any(c.isalpha() for c in s) and any(c.isdigit() for c in s). isalnum() alone accepts all-letter and all-digit strings.

Keep learning

Step-by-step lessons:
Python StringsPython String Methods

Related:
Python String isalpha() MethodPython String isdigit() MethodPython Regular Expressions (regex)How to Remove Whitespace from a String in Python

More in string methods:
Python String split() MethodPython String join() MethodPython String strip() MethodPython String lstrip() MethodPython String rstrip() MethodPython String replace() MethodPython String upper() MethodPython String lower() Method See all →