Summary: in this tutorial, you’ll learn how to use the Python string isalnum()
method to check if all characters in the string are alphanumeric.
Introduction to the Python string isalnum() method
The string isalnum()
method returns True
if:
- all characters in the string are alphanumeric
- and the string has at least one character.
Otherwise, it returns False
.
The following shows the syntax of the isalnum()
method:
str.isalnum()
Code language: CSS (css)
Functionally, the string str
contains only alphanumeric characters if one of the following methods returns True
:
Python string isalnum() examples
The following example uses the isalnum()
method to check if the string 'Python3'
contains only alphanumeric characters:
version = 'Python3'
result = version.isalnum()
print(result)
Code language: PHP (php)
Output:
True
Code language: PHP (php)
It returns True
because the string 'Python3'
contains only letters and numbers.
The following example returns False
because the string 'Python 3'
contains a whitespace:
version = 'Python 3'
result = version.isalnum()
print(result)
Code language: PHP (php)
Output:
False
Code language: PHP (php)
Summary
- Use the Python string
isalnum()
method to determine if all characters in the string are alphanumeric.
Did you find this tutorial helpful ?