Python .isalnum()

MamtaWardhani's avatar
Published Sep 27, 2023Updated Jul 15, 2025
Contribute to Docs

The .isalnum() method is a built-in string method in Python that checks whether all characters in a string are alphanumeric. This method returns True if every character in the string is either a letter (a-z, A-Z) or a digit (0-9), and the string contains at least one character. If any character is not alphanumeric or if the string is empty, the method returns False.

The .isalnum() method is commonly used in data validation scenarios, such as checking usernames, passwords, or any input that should only contain letters and numbers. It’s particularly useful in form validation, data cleaning operations, and when processing user inputs, where special characters need to be filtered out.

  • Looking for an introduction to the theory behind programming? Master Python while learning data structures, algorithms, and more!
    • Includes 6 Courses
    • With Professional Certification
    • Beginner Friendly.
      75 hours
  • Learn the basics of Python 3.12, one of the most powerful, versatile, and in-demand programming languages today.
    • With Certificate
    • Beginner Friendly.
      24 hours

Syntax

string.isalnum()

Parameters:

  • This method does not accept any parameters.

Return value:

  • Returns True if all characters in the string are alphanumeric (letters and digits)
  • Returns False if the string is empty or contains at least one non-alphanumeric character

Example 1: Basic String Validation

The following example demonstrates the fundamental usage of .isalnum() to check if a string contains only letters and numbers:

# Check different types of strings
text1 = "Python123" # Contains letters and numbers
text2 = "HelloWorld" # Contains only letters
text3 = "123456" # Contains only numbers
# Test each string
print(f"'{text1}' is alphanumeric: {text1.isalnum()}")
print(f"'{text2}' is alphanumeric: {text2.isalnum()}")
print(f"'{text3}' is alphanumeric: {text3.isalnum()}")
# Test strings with special characters
invalid_text = "Python 123" # Contains space
print(f"'{invalid_text}' is alphanumeric: {invalid_text.isalnum()}")

The output generated by this code is:

'Python123' is alphanumeric: True
'HelloWorld' is alphanumeric: True
'123456' is alphanumeric: True
'Python 123' is alphanumeric: False

This example shows how .isalnum() returns True for strings containing only letters, only numbers, or a combination of both, but returns False when spaces or special characters are present.

Example 2: Username Validation System

The following example demonstrates a practical real-world application of .isalnum() for validating user input in a registration system:

def validate_username(username):
"""
Validate username using isalnum() method
Username should only contain letters and numbers
"""
if not username: # Check if empty
return "Username cannot be empty"
elif len(username) < 3:
return "Username must be at least 3 characters long"
elif len(username) > 20:
return "Username must be no more than 20 characters long"
elif not username.isalnum():
return "Username can only contain letters and numbers"
else:
return "Valid username"
# Test different usernames
usernames = ["John123", "user_name", "test@gmail", "Python", "ab", "validuser2024"]
print("Username Validation Results:")
print("-" * 40)
for username in usernames:
result = validate_username(username)
print(f"'{username}': {result}")

The output generated by this code is:

Username Validation Results:
----------------------------------------
'John123': Valid username
'user_name': Username can only contain letters and numbers
'test@gmail': Username can only contain letters and numbers
'Python': Valid username
'ab': Username must be at least 3 characters long
'validuser2024': Valid username

This example shows how .isalnum() can be integrated into a comprehensive validation function to ensure usernames meet specific criteria for web applications or user registration systems.

Codebyte Example: Data Cleaning and Filtering

The following example demonstrates using .isalnum() for data processing tasks, such as filtering valid product codes from a dataset:

Code
Output
Loading...

This example illustrates how .isalnum() can be used in data processing workflows to clean and validate data, making it particularly useful for ETL operations or data quality checks.

Frequently Asked Questions

1. Does .isalnum() return True for an empty string?

No, .isalnum() returns False for empty strings. The string must contain at least one character, and all characters must be alphanumeric.

2. Does .isalnum() work with Unicode characters?

Yes, .isalnum() works with Unicode letters and digits, not just ASCII characters.

3. What’s the difference between .isalnum() and .isalpha() or .isdigit()?

.isalpha() checks if all characters are letters only, .isdigit() checks if all characters are digits only, while .isalnum() checks if all characters are either letters or digits (or a combination of both).

All contributors

Contribute to Docs

Learn Python on Codecademy

  • Looking for an introduction to the theory behind programming? Master Python while learning data structures, algorithms, and more!
    • Includes 6 Courses
    • With Professional Certification
    • Beginner Friendly.
      75 hours
  • Learn the basics of Python 3.12, one of the most powerful, versatile, and in-demand programming languages today.
    • With Certificate
    • Beginner Friendly.
      24 hours