In this Python tutorial, we will dive deep into understanding the isalnum method in Python string, leveraging some examples for a comprehensive explanation. And we will also see the limits and places where isalnum() is beneficial.
Python, one of the world’s most popular programming languages, has a host of built-in string methods that offer an easy and efficient way to manipulate and evaluate strings. One such string method is isalnum().
What is the isalnum method in Python string
The isalnum() method is a built-in method applicable to strings in Python. Its primary function is to check if all the characters in a given string are alphanumeric (comprising only letters and numbers).
In other words, the method will return True if all characters in the string are either alphabets or numbers and there is at least one character. Otherwise, it returns False.
Syntax:
The syntax of the isalnum() function in Python:
str.isalnum()
The str is the Python string on which the operation is going to be held.
Parameters:
The Python isalnum method does not take any Parameter
Return values:
The isalnum() string method in Python returns:
- True – If all characters in the given Python string are alphanumeric (only alphabets or numbers)
- False – If at least one character is not alphanumeric in the given Python string.
Python isalnum in string method examples
There can be many different applications of the Python isalnum() method in the string which can be taken into consideration:
- Data Validation: Imagine building a system on Python that requires alphanumeric usernames based on the combination of the user’s state and ZIP code. The string isalnum() method can be used to validate such inputs quickly.
- Password Restrictions: Websites, may have password requirements that mandate the use of alphanumeric characters. Python developers can employ isalnum() to ensure password conformity.
- Case Sensitivity: The isalnum method in Python string is case-insensitive. This means that both uppercase and lowercase alphabets are treated the same.
Some of them are explained with the help of examples:
Example-1: The basic isalnum() Python string method
Let’s take an example in Python: ZIP codes and State names. ZIP codes are numbers, while state names comprise alphabets. A combination, such as the abbreviation of a state followed by a ZIP code, would be alphanumeric.
zip_code = "90210"
state_name = "California"
combination = "CA90210"
print(zip_code.isalnum())
print(state_name.isalnum())
print(combination.isalnum())
The output is:
True
True
True
- zip_code contains only numbers, so isalnum() returns True.
- state_name contains only alphabets, so isalnum() returns True.
- combination contains both alphabets and numbers, so isalnum() returns True.
Note: If there are spaces or any other special characters, the Python isalnum() string method would return False.
state_with_space = "New York"
print(state_with_space.isalnum())
The output is: Here, “New York” has a space, so isalnum() returns,
False
This way we can validate data with the isalnum() string function in Python.
Example-2: Using isalnum in if-else statements in Python
Let’s consider a scenario, Imagine a software system made on Python for a library in Chicago that requires users to create a username for their library account. To ensure simplicity, the library mandates that usernames should only be alphanumeric.
Here’s how the Python isalnum method can be used to verify the validity of the usernames:
def validate_username(username):
if username.isalnum():
return "Username is valid."
else:
return "Username is invalid. Only alphanumeric characters are allowed."
# Sample tests
print(validate_username("JohnDoe2023"))
print(validate_username("John_Doe"))
print(validate_username("AliceInLibrary123"))
print(validate_username("Bob@Books"))
The Output is:
In this example, “JohnDoe2023” and “AliceInLibrary123” are accepted as they only have letters and numbers. On the other hand, “John_Doe” and “Bob@Books” are rejected due to the presence of non-alphanumeric characters (‘_‘ and ‘@‘ respectively) by isalmun method in the Python string.
Username is valid.
Username is invalid. Only alphanumeric characters are allowed.
Username is valid.
Username is invalid. Only alphanumeric characters are allowed.
This way we can use isalnum String method with the if-else conditional statement in Python.
Read: Isdigit method in String Python
There can be limitations in the isalnum() string method in Python that can be taken into consideration :
- Whitespace and Special Characters: If a string contains spaces or special characters like !, @, %, etc., the isalnum() method will return False.
- Empty Strings: The isalnum() method returns False for an empty string.
- Localization: The behavior of the isalnum() method might vary with different locales and Unicode characters. When working with non-ASCII characters, be aware of potential inconsistencies.
Conclusion
The string isalnum() method in Python provides a straightforward way to check if a string consists only of alphanumeric characters. This is particularly useful in numerous real-world scenarios, which we have seen in the section of isalnum Python string examples. We have also seen where the isalnum string method has its limit.
You may also like to read:
- Isidentifier method in String Python
- Isascii method in String Python
- Isalpha method in Python string
- Isalnum method in Python string
- Isdecimal method in Python String
I am Bijay Kumar, a Microsoft MVP in SharePoint. Apart from SharePoint, I started working on Python, Machine learning, and artificial intelligence for the last 5 years. During this time I got expertise in various Python libraries also like Tkinter, Pandas, NumPy, Turtle, Django, Matplotlib, Tensorflow, Scipy, Scikit-Learn, etc… for various clients in the United States, Canada, the United Kingdom, Australia, New Zealand, etc. Check out my profile.