Python String isalnum()
Python String isalnum() method returns True if each of the character in given string is either alphabet letter (a-zA-Z) or number (0-9).
If the string contains one or more characters that does not belong to any of the above specified group, then isalnum() returns False.
In this tutorial, we will learn the syntax and examples for isalnum() method of String class.
Syntax
The syntax of String isalnum() method in Python is
str.isalnum()
Example
In this example, we will take a string 'abcdABC123'
, and check if this string is alphanumeric using str.isalnum()
method.
Python Program
x = 'abcdABC123'
result = x.isalnum()
print(result)
Output
True
String contains non-alphanumeric
In this example, we will take a string 'abcd@123'
, and check if this string is alphanumeric using str.isalnum()
method. Since the string contains a non-alphanumeric character @
, isalnum() returns False.
Python Program
x = 'abcd@123'
result = x.isalnum()
print(result)
Output
False
Conclusion
In this Python Tutorial, we learned how to check if given string is alphanumeric, using String method – isalnum().