Python str.isalnum() function
In Python, the str.isalnum()
method is used to check if all characters in a string are alphanumeric, meaning that they are either letters (a-z, A-Z) or digits (0-9). This method returns True
if the string contains at least one character and all characters are alphanumeric; otherwise, it returns False
.
Syntax
Example Usage
- Basic usage with letters and digits:
- Including spaces:
- With special characters:
- Checking an empty string:
- Using non-English alphanumeric characters:
- Only letters:
- Only digits:
Important Notes
- The
isalnum()
method will returnFalse
if the string contains any characters that are not letters or digits, including whitespace and punctuation. - It considers Unicode characters as well, so letters from various languages and scripts are included as valid alphanumeric characters.
Summary
- Use
str.isalnum()
to check if a string consists entirely of alphanumeric characters. - It returns
True
for strings made up of letters and digits (including accented characters) andFalse
for strings that contain any non-alphanumeric characters.