Introduction: Python String isalnum() Method
When working with strings, you often need to check whether they contain only letters and numbers—no spaces, punctuation, or symbols. The Python Stringisalnum() method in Python provides a simple way to perform this check efficiently.
What it is: It is a built-in Python string method that returns True if all characters in the string are letters or digits, and False otherwise. This includes both uppercase and lowercase letters (A–Z, a–z) and digits (0–9). The method always returns False for empty strings.
You’ll use isalnum() to quickly validate input, filter text, or clean datasets. It is especially handy when checking usernames, IDs, codes, or tokens before processing them further.
Let’s now explore the syntax of isalnum() and see it in action.
Browse More String Methods: Want to learn what other string methods can do? Visit the Python String Methods List
Python String isalnum() Method: Syntax, Parameters, Return Values and Examples
Syntax
string.isalnum()
The method takes no parameters. — it simply examines the string it’s called on.
Return Value
True— all characters are letters or digits, and the string is not emptyFalse— any other character appears, or the string is empty
The method supports Unicode: accented letters like “é” count as alphabetic.
Quick Example
username = "User123"
print(username.isalnum())
# Output: True
code = "User_123"
print(code.isalnum())
# Output: False
The first string contains only letters and numbers, so it returns True. The second string has an underscore, so it returns False.
Examples of Python String isalnum() Method
Here are some practical examples showing how the isalnum() method behaves with different kinds of strings:
Example 1: Basic alphanumeric string with letters and digits
s = "Python3"
print(s.isalnum())
# Output: True
Explanation:
The isalnum() method returns True because “Python3” contains only letters and digits, which are valid alphanumeric characters.
Example 2: String containing a space character
s = "Python 3"
print(s.isalnum())
# Output: False
Explanation:
The isalnum() method returns False because spaces are not considered alphanumeric characters.
Example 3: String with special symbols
s = "Data@123"
print(s.isalnum())
# Output: False
Explanation:
The “@” symbol disqualifies the string from being alphanumeric, so isalnum() returns False.
Example 4: String containing only numeric characters
s = "2025"
print(s.isalnum())
# Output: True
Explanation:
Numbers alone are considered alphanumeric, so the method returns True.
Example 5: String containing only letters
s = "OpenAI"
print(s.isalnum())
# Output: True
Explanation:
All letters are valid alphanumeric characters, so the method returns True.
Example 6: Empty string with no characters
s = ""
print(s.isalnum())
# Output: False
Explanation:
Empty strings contain no characters, so isalnum() returns False.
Example 7: String with Unicode letters and digits
s = "Café123"
print(s.isalnum())
# Output: True
Explanation:
Unicode letters like “é” and digits are valid, so the method returns True.
Example 8: String containing newline or tab characters
s = "Python\n3"
print(s.isalnum())
# Output: False
Explanation:
Control characters such as newline or tab are not letters or digits, so the method returns False.
Common Use Cases: Python String isalnum() Method
Practical scenarios where isalnum() is useful for validation and data checks.
| Scenario | Use of isalnum() |
|---|---|
| Validating user input | Ensure usernames or IDs contain only letters and numbers |
| Text preprocessing | Filter tokens that contain unwanted characters |
| Cleaning datasets | Remove entries containing symbols or punctuation |
| Identifier validation | Verify naming rules for IDs or codes |
Comparison with Similar Methods
See how isalnum() differs from other string-check methods.
| Method | Description |
|---|---|
| isalpha() | True if only alphabetic characters (no digits) |
| isdigit() | True if only numeric digits (no letters) |
| isalnum() | True if only letters or digits |
| isspace() | True if only whitespace characters |
Summary Table: Python String isalnum() Method
A quick reference showing inputs and outputs of isalnum().
| Input String | Output | Reason |
|---|---|---|
| “Python3” | True | Only letters and digits |
| “12345” | True | All digits |
| “Open AI” | False | Contains space |
| “Test@123” | False | Special character |
| “” | False | Empty string |
| “Café456” | True | Unicode letters valid |
Key Takeaways: Python String isalnum() Method
- Checks if string contains only letters and numbers
- Returns False for empty strings
- Useful for validating usernames or IDs
- Spaces, punctuation, and control characters all return
False. - Helps clean data or filter text
- No parameters are required