Python String isalnum() Method
The isalnum() method detects whether a string is composed of letters and numbers.
Python String isalnum() Syntax
Syntax of the isalnum() method.
str.isalnum()
Python String isalnum() Parameters
- None.
Python String isalnum() Return Value
True if string has at least one character and all characters are letters or numbers, otherwise False
Python String isalnum() Example#1
The following example shows an instance of the isalnum() method.
string = "apidemos 123"
print(string, "is alphanumeric?", string.isalnum()) # apidemos 123 is alphanumeric? False
string = "apidemos_123"
print(string, "is alphanumeric?", string.isalnum()) # apidemos_123 is alphanumeric? False
string = "000"
print(string, "is alphanumeric?", string.isalnum()) # 000 is alphanumeric? True
string = "bbbb"
print(string, "is alphanumeric?", string.isalnum()) # bbbb is alphanumeric? True
# We can also use Python String isalnum() Method along with if…else statements, to output custom messages
password = "apidemos123456"
if password.isalnum():
print("Password is alphanumeric.")
else:
print("Password is not alphanumeric.")
Output: