The isalnum()
method is a built-in method in Python that checks whether all characters in a string are alphanumeric (a-z, A-Z, 0-9) and returns a boolean value (True or False).
Syntax
The syntax of the isalnum()
method is as follows:
string.isalnum()
Here, string
is the string to be checked for alphanumeric characters.
Return Value
The isalnum()
method returns a boolean value (True or False). It returns True if all characters in the string are alphanumeric and False if the string contains at least one non-alphanumeric character.
Examples
Here are three different examples of how to use the isalnum()
method in Python:
Example 1: Checking If a String Contains Only Alphanumeric Characters
The following example demonstrates how to use the isalnum()
method to check if a string contains only alphanumeric characters:
string = "Hello123" result = string.isalnum() print(result)
Output:
True
In this example, we define a string that contains both letters and digits, and then use the isalnum()
method to check if all the characters in the string are alphanumeric. Since all the characters are alphanumeric, the method returns True.
Example 2: Checking If a String Contains Non-Alphanumeric Characters
The following example demonstrates how to use the isalnum()
method to check if a string contains non-alphanumeric characters:
string = "Hello123!" result = string.isalnum() print(result)
Output:
False
In this example, we define a string that contains both letters, digits, and a non-alphanumeric character (!), and then use the isalnum()
method to check if all the characters in the string are alphanumeric. Since the string contains a non-alphanumeric character, the method returns False.
Example 3: Using isalnum() with User Input
The following example demonstrates how to use the isalnum()
method with user input:
string = input("Enter a string: ") result = string.isalnum() if result: print("The string contains only alphanumeric characters.") else: print("The string contains at least one non-alphanumeric character.")
In this example, we use the built-in input()
function to take a string input from the user. We then use the isalnum()
method to check if the string contains only alphanumeric characters. If it does, we print a message indicating that the string contains only alphanumeric characters. If it doesn’t, we print a message indicating that the string contains at least one non-alphanumeric character.
Use Cases
The isalnum()
method is useful in situations where you need to check whether a string contains only alphanumeric characters. This can be useful in input validation, where you want to ensure that a user has entered only alphanumeric characters in a form field. It can also be useful in data cleaning, where you want to remove any non-alphanumeric characters from a string before performing operations on it. Additionally, the isalnum()
method can be used to filter out non-alphanumeric characters from a string, by iterating through each character in the string and appending only the alphanumeric characters to a new string or list.