Understanding isalnum() Method in Python

Understanding isalnum() Method in Python

5 mins read407 Views Comment
Updated on Apr 24, 2023 02:26 IST

This blog explains isalnum() Method in Python with the help of examples. Let’s explore!

2023_04_Copy-of-Copy-of-Feature-Image-Templates-25.jpg

The function isalnum() in Python programming is utilized to verify if all the characters within a string are either numbers (0-9) or letters (a-z or A-Z). This method does not require any input parameters and will return a boolean value (True or False) based on the examination of the string.  

Explore free Python courses

In this article, we will discuss this function and understand its use with the help of examples. We will be covering the following sections: 

Explore Online Python Courses

Introduction to isalnum() Method in Python 

In Python programming, the function isalnum() checks whether a given string contains only alphabetic or numeric characters, or a combination of both. The function returns a boolean value, i.e., True or False, based on the evaluation of the string. 

The isalnum() function is a part of the string module and can be applied to any string variable or literal. This method checks every character in the string and returns True if all the characters are either alphabetic or numeric or a combination of both. Otherwise, it returns False.  

Syntax of isalnum() 

The syntax of isalnum() method in Python is given as: 

 
string.isalnum()
Copy code

Parameters of isalnum() 

To utilize isalnum() in Python, you must call it on a string variable or literal. This method does not require any input parameters, and its purpose is to verify whether the string calling the method includes only alphabetic or numeric characters.  

Return Value of isalnum() 

After examining a string to determine if it contains only alphabetic or numeric characters, the isalnum() method returns a boolean value.  

Python Dependency Management Made Simple
Learn the max() Function in Python with Examples
Types of Functions in Python
Recommended online courses

Best-suited Python courses for you

Learn Python with these high-rated online courses

Free
6 weeks
– / –
2 weeks
– / –
16 weeks
1.7 K
3 months
– / –
– / –
4.24 K
2 weeks
3 K
3 weeks
– / –
4 months

Examples of Using the isalnum() Method in Python 

Example 1: Password Validation 

You can use the isalnum() method to validate user passwords in Python. In this example, you can prompt the user to enter a password and then check if the password contains only alphabetic or numeric characters. 

 
password = input("Enter your password: ")
if password.isalnum():
print("Valid Password")
else:
print("Invalid Password")
Copy code

Output: 

Enter your password: sudoku

Valid Password

Example 2: Removing Punctuation from a String 

If you have a string containing punctuation marks, you can use the isalnum() method to remove them. In this example, you can create a new string containing only the original string’s alphanumeric characters. 

 
string = "Hello, learner 20!"
new_string = ""
for char in string:
if char.isalnum():
new_string += char
print(new_string)
Copy code

Output: 

Hellolearner20 

Example 3: Checking if a String is a Valid Identifier 

In Python, an identifier is a name used to identify a variable, function, class, or any other object. You can use the isalnum() method to check if a given string is a valid Python identifier. 

 
identifier = input("Enter an identifier: ")
if identifier.isalnum() and not identifier[0].isdigit():
print("Valid Identifier")
else:
print("Invalid Identifier")
Copy code

Output: 

Enter an identifier: 56type 

Invalid Identifier 

Example 4: Checking if a String contains only Letters 

If you want to check if a string contains only letters (both uppercase and lowercase), you can use the isalpha() method along with the isalnum() method. The isalpha() method returns True if all the characters in the string are alphabets, and the isalnum() method returns True if all the characters in the string are either alphabets or numbers. 

 
string = input("Enter a string: ")
if string.isalpha() or string.isalnum():
print("Contains only letters")
else:
print("Does not contain only letters")
Copy code

Output: 

Enter a string: H@ve a great day 
Does not contain only letters 

Example 5: Generating Random Alphanumeric Strings 

You can use the isalnum() method and other Python string methods to generate random alphanumeric strings. In this example, we use the random module to generate a random 8-character string that contains only letters and numbers. 

 
import random
import string
random_string = ''.join(random.choices(string.ascii_letters + string.digits, k=8))
if random_string.isalnum():
print(random_string)
Copy code

Output: 

9redLex9 

Example 6: Validating Usernames 

You can use the isalnum() method to validate usernames in Python. In this example, we prompt the user to enter a username, and then we check if the username contains only alphabetic or numeric characters and is between 4 to 16 characters long. 

 
username = input("Enter your username: ")
if username.isalnum() and len(username) >= 4 and len(username) <= 16:
print("Valid Username")
else:
print("Invalid Username")
Copy code

Output: 

Enter your username: lucifer 
Valid Username 

These are just a few examples of how the isalnum() method can be used in Python programming to validate input, detect languages, and clean text data. 

Endnotes 

The isalnum() method is a useful string method in Python that can be used to check whether a given string is alphanumeric. By understanding the isalnum() method and its usage, you can perform various operations on strings in Python and write more efficient and robust code. With practice and experience, you can also use this method to create more complex programs and applications that deal with string manipulation. 

Hope this article was helpful for you. You can explore related articles here if you wish to learn more about Python and practice Python programming.

FAQs

What does the isalnum() method do in Python?

The isalnum() method is a string method in Python that returns True if all the characters in the string are either alphabetic or numeric.

What is the return type of the isalnum() method in Python?

The isalnum() method returns a boolean value, either True or False, based on the evaluation of the string.

Does the isalnum() method consider spaces and other special characters?

No, the isalnum() method only considers alphabetic and numeric characters. Spaces and other special characters are not considered alphanumeric and will cause the method to return False.

Does the isalnum() method modify the original string?

No, the isalnum() method does not modify the original string. It only evaluates the characters in the string and returns a boolean value.

How can I use the isalnum() method to check if a string contains only letters in Python?

You can use the isalpha() method along with the isalnum() method to check if a string contains only letters in Python. The isalpha() method returns True if all the characters in the string are alphabets, and the isalnum() method returns True if all the characters in the string are either alphabets or numbers.

How can I use the isalnum() method to check if a string contains only numbers in Python?

You can use the isdigit() method along with the isalnum() method to check if a string contains only numbers in Python. The isdigit() method returns True if all the characters in the string are digits, and the isalnum() method returns True if all the characters in the string are either alphabets or numbers.

About the Author

This is a collection of insightful articles from domain experts in the fields of Cloud Computing, DevOps, AWS, Data Science, Machine Learning, AI, and Natural Language Processing. The range of topics caters to upski... Read Full Bio