The python string isalnum() method is used to check whether the string consists of alphanumeric characters. This method returns true if all the characters in the input string are alphanumeric and there is at least one character. Otherwise, it returns false.
A character 'char' is considered to be alphanumeric if it returns true for all these functions: char.isalpha(), char.isdecimal(), char.isdigit(), or char.isnumeric(). That is, the character should be either lowercase alphabet, uppercase alphabet, a number/digit, or a decimal. Except these, the other categories are not considered to be alphanumeric.
Let us look into this function with more details in the following section.
SyntaxFollowing is the syntax for the python string isalnum() method −
str.isalnum()Parameters
The python string isalnum() method does not contain any parameters.
Return ValueThe python string isalnum() method returns true if all characters in the string are alphanumeric and there is at least one character and false otherwise.
ExampleThe lowercase, uppercase alphabets and numbers come under alphanumeric characters.
The following is an example of the python string isalnum() method. In this we are trying to find out whether the string "tutorial" is alpha numeric.
str = "tutorial" result=str.isalnum() print("Are all the characters of the string alphanumeric?", result)
On executing the above program, the following output is generated -
Are all the characters of the string alphanumeric? TrueExample
Only the lowercase, uppercase alphabets and numbers come under alphanumeric characters. Other characters such as '!', '.', '/', etc.. are not alphanumeric characters. Even if the string containing alphabets has these kind of non-alphanumeric characters, the isalnum() function returns false.
str = "Hello!Welcome." result=str.isalnum() print("Are all the characters of the string alphanumeric?", result)
The following is the output obtained by executing the above program -
Are all the characters of the string alphanumeric? FalseExample
Only the lowercase, uppercase alphabets and numbers come under alphanumeric characters. Other characters such as '!', '.', '/', '@', '#' etc.. are not alphanumeric characters. When the isalnum() method is applied on such strings containing those characters, it returns false.
str = "#@%$" result=str.isalnum() print("Are all the characters of the string alphanumeric?", result)
The following output is obtained by executing the above program -
Are all the characters of the string alphanumeric? FalseExample
Only the lowercase, uppercase alphabets and numbers come under alphanumeric characters. Other characters such as '!', '.', '/', etc.. are not alphanumeric characters.
str = "Aa12" result=str.isalnum() print("Are all the characters of the string alphanumeric?", result)
The above program, on executing, displays the following output -
Are all the characters of the string alphanumeric? TrueExample
The lowercase alphabets, uppercase alphabets, and the numbers are considered as alphanumeric characters. Even the empty space " " is also not considered alphanumeric.
str = "1234567189 " result=str.isalnum() print("Are all the characters of the string alphanumeric?", result)
The output of the above program is displayed as follows -
Are all the characters of the string alphanumeric? False
python_strings.htm
RetroSearch is an open source project built by @garambo | Open a GitHub Issue
Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo
HTML:
3.2
| Encoding:
UTF-8
| Version:
0.7.4