Python String isalnum() Method



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.

Syntax

Following is the syntax for the python string isalnum() method −

str.isalnum()

Parameters

The python string isalnum() method does not contain any parameters.

Return Value

The python string isalnum() method returns true if all characters in the string are alphanumeric and there is at least one character and false otherwise.

Example

The 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.

#!/usr/bin/python
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? True

Example

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.

#!/usr/bin/python
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? False

Example

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.

#!/usr/bin/python
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? False

Example

Only the lowercase, uppercase alphabets and numbers come under alphanumeric characters. Other characters such as '!', '.', '/', etc.. are not alphanumeric characters.

#!/usr/bin/python
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? True

Example

The lowercase alphabets, uppercase alphabets, and the numbers are considered as alphanumeric characters. Even the empty space " " is also not considered alphanumeric.

#!/usr/bin/python
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
Advertisements