
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
How to write Python regular expression to check alphanumeric characters?
This article will explain you how to write Python regular expression to check alphanumeric characters. Alphanumeric characters in Python are defined as letters like (a-z, A-Z) and numbers (0-9).
If you want to check that the string has only alphanumeric characters like letters and numbers you can use Python's re module which makes this task easy.
Regex Pattern for Alphanumeric Check
Here is the regex pattern for checking alphanumeric characters -
^[a-zA-Z0-9]+$
The pattern (a-z) represents lowercase letters, (A-Z) means uppercase letters and (0-9) represents numbers from 0 to 9. And "^" is start of the string, "+" is used to make sure there is at least one character and dollar sign $ represents the end of the string.
Examples to check Alphanumeric Characters
There are several ways to check if a string contains only alphanumeric characters, such as ?
Example: Basic Regex Check
The following example will use the re module to check if a string is alphanumeric. The re.match() function is used to match the regex pattern against the string. If the string matches the pattern, it returns True, otherwise it returns False.
# import re module import re # define a function to check if a string is alphanumeric def checkAlphaChar(txt): # use regex to check if the string is alphanumeric return bool(re.match(r"^[a-zA-Z0-9]+$", txt)) # text the function and print the result print(checkAlphaChar("Python123")) print(checkAlphaChar("Hello Tutorialspoint!"))
Output
This will create the below outcome -
True False
Example: Check Empty String
Here we will try to handle empty input. If the string is empty so it should returns False. With the help of this function we can have an extra check to handle empty strings before applying regex to check alphanumeric character.
# import re module import re # define a function to checkAlphaChar if a string is alphanumeric def checkAlphaChar(txt): if txt: return bool(re.match(r"^[a-zA-Z0-9]+$", txt)) else: return False # test the function with different inputs print(checkAlphaChar("")) print(checkAlphaChar("Year2025"))
Output
This will generate the below result -
False True
Example: Check Multiple Inputs
Now, we will add multiple inputs using a list. We will iterate over the list and check each string using the regex pattern. If the string matches the pattern, it will be added to the result list.
# import re module import re # define a function to checkAlphaChar if a string is alphanumeric def checkAlphaChar(lst): result = [] for txt in lst: if re.match(r"^[a-zA-Z0-9]+$", txt): result.append(txt) else: # do nothing for non-matching items pass return result # define a list of strings to check words = ["Hello123", "Tutorialspoint!", "Python2025", "TP@"] # test the function and print the result print(checkAlphaChar(words))
Output
This will produce the following result -
['Hello123', 'Python2025']
Example: Check with User Input
In this example we will accept a user input and check that it is valid alphanumeric character or not. If the input is valid it returns "Valid Input" and if it is not valid it returns "Invalid Input".
# import re module import re # define a function to checkAlphaChar if a string is alphanumeric def checkAlphaChar(): txt = input("Enter text: ") if re.match(r"^[a-zA-Z0-9]+$", txt): print("Valid Input!") else: print("Invalid Input!") # Run the function checkAlphaChar()
Output
This will lead to the following outcome -
Enter text: jb1k338y2 Valid Input! Enter text: avsd28@434y& Invalid Input!