Code360 powered by Coding Ninjas X Naukri.com. Code360 powered by Coding Ninjas X Naukri.com
Last Updated: Mar 28, 2024
Easy

Python String isalnum() method

Author Shubham Das
0 upvote
gp-icon
Basics of python
Free guided path
7 chapters
99+ problems
gp-badge
Earn badges and level up

Introduction

Python provides many built-in functions for programmers to make the work easier for them. There are a lot of built-in functions for string. We can check whether all the characters in a given string are alphanumeric characters or not. We can check if all the characters in a string are alphanumeric (only alphabets or numbers) or not using the isalnum() function which is a built-in function in Python. 

isalnum python

What is Python String isalnum() Method?

In Python, isalnum() is a built-in string method. It checks if all the characters in a given string are alphanumeric.

  • Alphanumeric characters include letters (a-z, A-Z) and numbers (0-9).
     
  • If the string contains only alphanumeric characters (no spaces or special symbols), isalnum() returns True. Otherwise, it returns False.
Get the tech career you deserve, faster!
Connect with our expert counsellors to understand how to hack your way to success
User rating 4.7/5
1:1 doubt support
95% placement record
Akash Pal
Senior Software Engineer
326% Hike After Job Bootcamp
Himanshu Gusain
Programmer Analyst
32 LPA After Job Bootcamp
After Job
Bootcamp

Islanum() Syntax

The isalnum() method in Python has a very simple straightforward syntax. It consists of the "isalnum" keyword and a pair of brackets.

string.isalnum()

 

In the above code, "string" is the string we want to check for alphanumeric characters.

isalnum() Parameters

isalnum() method does not take any parameters other than the string itself that you want to check.

islanum() Return Value

The isalnum() method returns Boolean values. If the string contains only alphanumeric characters (alphabets and numbers), the isalnum() method returns the Boolean value True. On the other hand, if the line is not alphanumeric, it returns a Boolean value False.

Example 1 of Python String Islanum() Method

Let's see some examples:

Basic Usage

We can check if a string contains all alphanumeric characters ( no spaces ) using this.

Code

  • Python

Python

text1 = "CodingNinjas123"
text2 = "Coding Ninjas 3"
result1 = text1.isalnum()
result2 = text2.isalnum()
print(result1)
print(result2)

Output

output

Explanation

  • text1 contains only alphanumeric characters (letters and numbers), so result1 is True.
     
  • text2 contains spaces and a digit, which are not alphanumeric characters, so result2 is False.

Handling User Input

We can handle user input and verify it.

Code

  • Python

Python

userinput = input("Enter a username: ")
if userinput.isalnum():
print("Username is valid.")
else:
print("Username contains invalid characters.")

Output

output

Explanation

It is designed to accept user input for a username and then check if the entered username consists only of alphanumeric characters. If it does, it prints "Username is valid"; otherwise, it prints "Username contains invalid characters." This is a simple form of user input validation for usernames to ensure they meet certain criteria.

Example 2: isalnum() in if..else Statement

  • Python

Python

# Input string
user_input = input("Enter a string: ")

# Check if the input string contains only alphanumeric characters
if user_input.isalnum():
print("The input contains only alphanumeric characters.")
else:
print("The input contains non-alphanumeric characters.")

Output

Enter a string: rahul 22
The input contains non-alphanumeric characters.

Explanation

In this example, we prompt the user to input a string. We use the isalnum() method to check if the input string contains only alphanumeric characters. If the condition user_input.isalnum() evaluates to True, we print a message indicating that the input contains only alphanumeric characters. If the condition evaluates to False, we print a message indicating that the input contains non-alphanumeric characters.

Also check out Fibonacci Series in Python

Frequently Asked Questions

What is the difference between Isalnum and Isalpha?

In Python, isalpha() is a method used to check if a string contains only alphabetic characters. On the other hand, the isalnum() method is used to check if a given string contains only alphabetic and numeric characters.

What is the difference between Isdigit and isalnum in Python?

The isdigit() method checks if all the characters in a Python string are digits. On the other hand, the isalnum() method is used to check if the given string contains only alphabetic and numeric characters.

What is Isalnum () in Python?

isalnum() in Python is a string method used to check if all characters in a string are alphanumeric. It returns True if the string consists only of letters or numbers, and False otherwise.

How do you check if a character is alphanumeric in Python?

In Python, you can check if a character is alphanumeric by using the isalnum() method. Simply call character.isalnum(), where character is the character you want to check.

What is the difference between Isalnum and Isnumeric in Python?

The main difference between isalnum() and isnumeric() in Python is that isalnum() checks if all characters in a string are alphanumeric (letters or numbers), while isnumeric() checks if all characters in a string are numeric (numbers).

Conclusion

In this article, we discussed the python string isalnum() method. The isalnum() method in Python provides a convenient way to check whether a string contains only alphanumeric characters. By returning True if the string consists solely of letters or numbers, and False otherwise, this method proves useful in various applications, from input validation to data processing. 

You may refer to our Guided Path on Code Studios for enhancing your skill set on DSACompetitive ProgrammingSystem Design, etc. Check out essential interview questions, practice our available mock tests, look at the interview bundle for interview preparations, and so much more!

Happy Learning Ninja!

Previous article
Exploring the Rjust in Python
Next article
numpy.histogram() Method in Python
Guided path
Free
gridgp-icon
Basics of python
7 chapters
127+ Problems
gp-badge
Earn badges and level up
Live masterclass