How to check if a string is alphanumeric in Python?



In Python, strings can contain letters, numbers, or special characters. To check if a string is alphanumeric (contains only letters and numbers), we can use different methods. This article shows three simple ways to do that:

  • Using the isalnum() function
  • Using regular expressions
  • Using the isalpha() and isdigit() functions

Using the isalnum() Function

The isalnum() method returns True if all characters in the string are letters or digits; otherwise, it returns False.

Example: Basic Alphanumeric Check

In the example below, we take two strings and check if they contain only alphabets and numbers using 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())

The output is as follows:

Checking whether Tutorialspoint123 is alphanumeric
True
Checking whether Tutorialspoint@123 is alphanumeric
False

Example: Detecting Symbols

Here, we take a simple string and a string containing special characters and check whether they are alphanumeric 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())

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 using re.fullmatch() method. To do this, import the re library (which is included with Python by default) and pass the regular expression ^[a-zA-Z0-9]+$ as a parameter along with the string.

This method matches the strings that are the same as the specified pattern; in this case, it matches strings containing only letters and numbers. This expression returns False if there are any special characters in the string; otherwise, it returns True.

Example

In this example, we use regular expressions to check if the given strings are alphanumeric:

import re

str1 = "Tutorialspoint123"
str2 = "Tutorialspoint@123"

print("Checking whether", str1, "is alphanumeric")
print(bool(re.fullmatch('^[a-zA-Z0-9]+$', str1)))

print("Checking whether", str2, "is alphanumeric")
print(bool(re.fullmatch('^[a-zA-Z0-9]+$', str2)))

The output obtained is:

Checking whether Tutorialspoint123 is alphanumeric
True
Checking whether Tutorialspoint@123 is alphanumeric
False

Using isalpha() and isdigit() Functions

Another way to check if a string is alphanumeric is by verifying each character individually to see if it is an alphabet or a digit. In this approach, we use the built-in methods isalpha() and isdigit():

  • The isalpha() method checks whether a character is an alphabet.
  • The isdigit() method checks whether a character is a digit.

By combining these methods, we can confirm if every character in the string is either an alphabet or a number.

Example

In the example below, we define a function that checks each character individually. If every character is either an alphabet or a digit, the function returns True; otherwise, it returns False:

def stringCheck(string):
   for char in string:
      if not (char.isalpha() or char.isdigit()):
         return False
   return True

str1 = "Tutorialspoint123"
str2 = "Tutorialspoint@123"

print("Checking whether", str1, "is alphanumeric")
print(stringCheck(str1))

print("Checking whether", str2, "is alphanumeric")
print(stringCheck(str2))

The output is:

Checking whether Tutorialspoint123 is alphanumeric
True
Checking whether Tutorialspoint@123 is alphanumeric
False
Updated on: 2025-06-09T09:19:01+05:30

7K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements