The isalnum() method returns True when every character in a string is a letter or digit and the string is not empty. It is a quick way to validate usernames, product codes, and other input that should contain only alphanumeric characters—no spaces, punctuation, or symbols.
01
Letters + Digits
Both are allowed.
02
Returns bool
True or False result.
03
No Arguments
Call directly on str.
04
Empty = False
Zero-length fails check.
05
Input Validation
Usernames and codes.
06
vs isalpha()
Know related methods.
Fundamentals
Definition and Usage
In Python, isalnum() inspects each character in a string. If every character is a letter (a–z, A–Z, or Unicode letters) or a digit (0–9) and the string has at least one character, it returns True. Otherwise it returns False. For example, "Hello123".isalnum() is True, but "Hello 123".isalnum() is False because of the space.
💡
Beginner Tip
isalnum() means “is alphanumeric”—letters or numbers, or a mix of both. It does not allow spaces, hyphens, underscores, or punctuation. For usernames with underscores, you need a custom check or regular expressions.
Foundation
📝 Syntax
The isalnum() method takes no parameters:
python
string.isalnum()
Syntax Rules
string — any valid Python str object.
Return value — True if all characters are alphanumeric and the string is non-empty; otherwise False.
No arguments — passing arguments raises TypeError.
Read-only — the original string is never modified.
Unicode — letters from other languages (like é or ñ) count as letters in Python 3.
Reference
↩ Return Value
isalnum() always returns a boolean—never an integer or string. It returns True only when the string contains one or more characters and every character is a letter or digit. It returns False for empty strings, strings with spaces, and strings with any symbol or punctuation.
Username validation — reject names with spaces or special characters.
Product codes — verify SKU or serial numbers contain only letters and digits.
Form processing — quick check before saving user input to a database.
Password rules (partial) — as one step in a larger validation (note: symbols are often required in passwords).
Filtering data — skip or flag rows where a field is not alphanumeric.
🧠 How isalnum() Works
1
Python reads the string
You call isalnum() on a str object—literal or variable.
Input
2
Each character is checked
Python tests whether every character is a letter or digit. If the string is empty, the result is immediately False.
Scan
3
Boolean result is returned
True if all pass; False if any character fails or the string is empty.
Result
=
✅
Validation complete
Use the boolean in conditionals to accept or reject input.
Important
📝 Notes
Spaces, punctuation, underscores, and symbols all cause False.
An empty string "" always returns False—there must be at least one alphanumeric character.
Unicode letters (like é or ñ) count as letters in Python 3.
isalnum() does not check string length—combine it with len() for min/max rules.
Store the result in a variable if you need to use it multiple times on the same string.
Wrap Up
Conclusion
The isalnum() method is a simple, readable way to check whether a string contains only letters and digits. It returns a clear boolean result, which makes it ideal for validating usernames, codes, and other user input in everyday Python programs.
Remember that spaces and symbols fail the check, empty strings return False, and related methods like isalpha() and isdigit() apply stricter single-type rules.
Combine with len() for minimum/maximum length rules
Use isalnum() for simple letter-and-digit checks
Pick isalpha() or isdigit() when you need one type only
Give clear error messages when validation fails
❌ Don’t
Expect underscores or hyphens to pass isalnum()
Use it as the only password security check
Assume an empty string returns True
Pass arguments to isalnum()
Rely on it for complex format rules—use regex when needed
Summary
Key Takeaways
Knowledge Unlocked
Five things to remember about isalnum()
Use these points when validating strings in Python.
5
Core concepts
✅01
Letters + Digits
Both types allowed.
Purpose
🔢02
True / False
Boolean return only.
Return
🛠03
Zero Arguments
s.isalnum()
Syntax
👤04
Validate Input
Usernames and codes.
Use case
⚠05
Empty = False
No chars fails check.
Edge case
❓ Frequently Asked Questions
isalnum() returns True if every character in the string is a letter or digit and the string is not empty. It returns False if the string contains spaces, punctuation, symbols, or has no characters at all.
string.isalnum(). It takes no arguments and returns a boolean (True or False).
It returns False. An empty string has no alphanumeric characters, so isalnum() is always False for "".
No. A space is not a letter or digit, so "Hello 123".isalnum() returns False. Use strip() first if you need to ignore leading or trailing spaces.
isalnum() allows both letters and digits. isalpha() requires all characters to be letters only. isdigit() requires all characters to be digits only. For example, "abc123".isalnum() is True, but "abc123".isalpha() and "abc123".isdigit() are both False.
No. isalnum() only inspects the string and returns a boolean. The original string stays unchanged.