How to check if a string is alphanumeric in Python?



Strings are an array of characters that are used to represent a word or any sentence. Strings in Python can be easily used as it does not require any explicit declaration and can be declared without any specifier. Strings in python also have many inbuilt functions and methods to manipulate and access the strings. Since in python everything is an object even String is also an object of the String class and has many methods.

In this article, we are going to find out if a given string has only alphabets and numbers without any special symbols using python. We have many approaches for the above query. We will discuss three ways −

  • Using the isalnum() function

  • Using regular expressions

  • Using the isalpha() and isdigit() functions

Using the isalnum() function

The isalnum() function which is an inbuilt function of the string library. It returns the Boolean output as True or False. It returns True if every character of the string is an alphabet or a number.

Example 1

In the example given below, we are taking 2 strings str1 and str2, and checking if they contain any characters other than alphabets and numbers. We are checking with the help of the isalnum() function.

str1 = "Tutorialspoint123" str2 = "Tutorialspoint@123" print("Checking whether",str1,"is alphanumeric") print(str1.isalnum()) print("Checking whether",str2,"is alphanumeric") print(str2.isalnum())

Output

The output of the above program is,

('Checking whether', 'Tutorialspoint123', 'is alphanumeric')
True
('Checking whether', 'Tutorialspoint@123', 'is alphanumeric')
False

Example 2

Following is another example of this function. In here, we are taking simple strings and checking whether they are alphanumeric or not using the isalnum() method.

s1 = "123abc" s2 = "123#$%abc" print("Checking whether",s1,"is alphanumeric") print(s1.isalnum()) print("Checking whether",s2,"is alphanumeric") print(s2.isalnum())

Output

The output of the above program is,

('Checking whether', '123abc', 'is alphanumeric')
True
('Checking whether', '123#$%abc', 'is alphanumeric')
False

Using regular expressions

We can also check if a string is alphanumeric in Python using regular expressions. To use this, we just need to import the re library and install it if it is not pre-installed. After importing the re library, we can make use of the regular expression "^[a zA Z0 9]+$".

This will return False if there are any other special characters in the string other than Alphabets and Numbers, otherwise True is returned.

Example

In this example, we are using regular expressions for checking if the given strings are alphanumeric or not.

import re str1 = "Tutorialspoint123" str2 = "Tutorialspoint@123" print("Checking whether",str1,"is alphanumeric") print(bool(re.match('^[a zA Z0 9]+$', str1))) print("Checking whether",str2,"is alphanumeric") print(bool(re.match('^[a zA Z0 9]+$', str2)))

Output

The output for the above program is,

('Checking whether', 'Tutorialspoint123', 'is alphanumeric')
False
('Checking whether', 'Tutorialspoint@123', 'is alphanumeric')
False

Using the isalpha() and isdigit() functions

Another way to achieve this is by checking each character individually if it is an alphabet or number or any other character. In this approach, we will use the built in methods isalpha() and isdigit().

  • The isAlpha() method is used to verify whether all the characters of the current string are alphabets.

  • Similarly, the isdigit() method verifies whether all the characters of the current string are digits.

Using both the methods with an or operator we can verify for the alpha numeric values.

Example

In the example given below, we defined a function, and we are checking each character individually if it is an alphabet or a number. If each character is either an alphabet or a number, then true is returned from the function or else false will be returned.

def stringCheck(string): flag = True for i in string: if i.isalpha() or i.isdigit(): pass else: flag = False return flag str1 = "Tutorialspoint123" str2 = "Tutorialspoint@123" print("Checking whether",str1,"is alphanumeric") print(stringCheck(str1)) print("Checking whether",str2,"is alphanumeric") print(stringCheck(str2))

Output

The output of the above program is,

('Checking whether', 'Tutorialspoint123', 'is alphanumeric')
True
('Checking whether', 'Tutorialspoint@123', 'is alphanumeric')
True

Advertisements