str.isalnum()

str.isalnum()
Returns: bool · Added in v3.x · Updated March 13, 2026 · String Methods
strings validation alphanumeric

The isalnum() method checks whether all characters in a string are alphanumeric—meaning they are either letters (a-z, A-Z) or digits (0-9)—and that the string contains at least one character. This method is useful for validating user input, checking whether strings meet certain format requirements, and filtering data based on character types.

This is one of several character classification methods in Python. It returns a Boolean value, making it convenient for conditional checks and validation logic.

Syntax

str.isalnum()

Parameters

This method takes no parameters.

Examples

Basic usage

text = "Hello123"
print(text.isalnum())
# True

mixed = "Hello World"
print(mixed.isalnum())
# False (space is not alphanumeric)

numbers = "12345"
print(numbers.isalnum())
# True

letters = "Hello"
print(letters.isalnum())
# True

Empty string returns False

empty = ""
print(empty.isalnum())
# False

# This behavior differs from some other is* methods
# which may return True for empty strings in certain contexts

Special characters fail the check

with_symbol = "hello@123"
print(with_symbol.isalnum())
# False

with_underscore = "user_name"
print(with_underscore.isalnum())
# False

with_dash = "product-123"
print(with_dash.isalnum())
# False

Common Patterns

Validating usernames

def is_valid_username(username):
    """Check if username contains only alphanumeric characters."""
    if not username:
        return False
    return username.isalnum()

print(is_valid_username("john_doe"))
# False (underscore not allowed)
print(is_valid_username("johndoe123"))
# True
print(is_valid_username(""))
# False

Processing input fields

# Simulate form validation
fields = {
    "username": "alex2024",
    "postal_code": "12345",
    "phone": "555-1234",
    "coupon_code": "SAVE20"
}

for field, value in fields.items():
    if value.isalnum():
        print(f"{field}: valid (alphanumeric)")
    else:
        print(f"{field}: invalid (contains non-alphanumeric)")
# username: valid (alphanumeric)
# postal_code: valid (alphanumeric)
# phone: invalid (contains non-alphanumeric)
# coupon_code: invalid (contains non-alphanumeric)

Filtering lists of strings

words = ["hello", "world123", "test!", "python3", "code#"]

# Keep only alphanumeric strings
alphanumeric = [w for w in words if w.isalnum()]
print(alphanumeric)
# ['hello', 'world123', 'python3']

Combining with length checks

def is_strong_password(password):
    """Password must be alphanumeric and at least 8 characters."""
    if len(password) < 8:
        return False
    return password.isalnum()

print(is_strong_password("abc"))
# False (too short)
print(is_strong_password("password"))
# False (too short)
print(is_strong_password("pass1234"))
# True
print(is_strong_password("pass1234!"))
# False (has special character)

Behavior Notes

  • Returns False for empty strings—this is important to remember when validating optional input fields
  • Unicode letters and digits are also considered alphanumeric in Python 3
  • The method does not check for spaces because spaces are not alphanumeric

Unicode support

# Unicode alphanumeric characters work correctly
unicode_text = "αβγ123"
print(unicode_text.isalnum())
# True

chinese = "你好123"
print(chinese.isalnum())
# True

# These return False because they're not alphanumeric
emoji = "👋"
print(emoji.isalnum())
# False

See Also