The isalnum
method checks if all the characters of a string are alphanumeric. If the string contains only alphanumeric characters, then isalnum
returns True
; otherwise, it returns False
.
Alphanumeric characters include:
string.isalnum()
The function returns True
if all characters of the string are alphanumerical.
The function returns False
if even one character of the string is not an alphanumeric character or if the string is empty.
string = "123abc"
string.isalnum() #True
In the string above, 123abc
, all the characters are alphanumeric, so the function returns True
.
string = "123 abc"
string.isalnum() #False
In the string above, 123 abc
, the space is not an alphanumeric character, so isalnum()
returns False
.
string = ""
string.isalnum() #False
The isalnum
method returns False
for an empty string.
string = "½"
string.isalnum() #True
isalnum
returns True
for the ½
string because fractions are considered alphanumeric characters.
string = "123abc"print(string, " isalnum --> ", string.isalnum())string = "123 abc"print(string, " isalnum --> ", string.isalnum())string = ""print(string, "isalnum -->", string.isalnum())string = "½"print(string, "isalnum -->", string.isalnum())