The isalnum function in Python evaluates whether every character within a string is alphanumeric. A character is classified as alphanumeric if it is either a letter or a numeral. This method does not accept special characters, including spaces.
Syntax of String isalnum Method
It has the following syntax:
isalnum()
Parameters
No parameter is required.
Return
It returns either True or False.
Different Examples for Python String isalnum Method
Let’s examine a few examples of the isalnum method to gain a better understanding of its functionalities.
Example 1
Let’s explore an example to illustrate the functionality of the isalnum method in Python strings.
# Python isalnum() function example
# Variable declaration
str = "Welcome"
# Calling function
str2 = str.isalnum()
# Displaying result
print(str2)
Output:
Example 2
In the event that a space is detected anywhere within the string, the function will return False. Refer to the example provided below.
# Python isalnum() function example
# Variable declaration
str = "Welcome"
# Calling function
str2 = str.isalnum()
# Displaying result
print(str2)
Output:
Example 3
Allow us to illustrate the usage of the Python string method isalnum with an example.
# Python isalnum() function example
# Variable declaration
str = "Welcome123" # True
str3 = "Welcome 123" # False
# Calling function
str2 = str.isalnum()
str4 = str3.isalnum()
# Displaying result
print(str2)
print(str4)
Output:
True
False
Example 4
It will return True even if the string consists entirely of numbers. Refer to the example provided.
# Python isalnum() function example
# Variable declaration
str = "123456"
# Calling function
str2 = str.isalnum()
# Displaying result
print(str2)
Output: