Python string isalnum() method is a built-in function that returns True if all the characters in a string are alphanumeric. Otherwise, it returns false.
Example of characters that are not alphanumeric: (space)!#%&? etc.
What is Alphanumeric?
The alphanumeric is a string that consists of letters and numbers and often other symbols (such as punctuation marks and mathematical symbols).
Syntax
The syntax of isalnum()
method is:
string.isalnum()
Parameters
The isalnum()
method doesn’t take any parameters.
Return value
The isalnum()
method returns:
- True if all the characters in a string are either letters or numbers
- False if one or more characters are not alphanumeric.
Example 1: Python string isalnum() working examples
# string is Alphanumeric
text1 = "Python3"
print(text1.isalnum())
# string is Alphanumeric
text2= "abc123def456"
print(text2.isalnum())
# only alphabets and returns true
text3= "Itsmycode"
print(text3.isalnum())
# only numbers and returns true
text4="1234567"
print(text4.isalnum())
# Contains space returns false
text5= "Python Tutorials123"
print(text5.isalnum())
Output
True
True
True
True
False
Example 2: Program to check if the string is alphanumeric
# Alphanumeric string
text1 = "Python123Tutorials"
if(text1.isalnum()):
print("String is Alphanumeric")
else:
print("String is not Alphanumeric")
# Non Alphanumeric String
text2= "hello World"
if(text2.isalnum()):
print("String is Alphanumeric")
else:
print("String is not Alphanumeric")
Output
String is Alphanumeric
String is not Alphanumeric