- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
What is the Python regular expression to check if a string is alphanumeric?
In this article, we are going to focus on how to check if a string is alphanumeric using Regular expressions in Python.
Regular expressions are used in both the techniques. Import the re library and install it if it isn't already installed to use it. After importing the re library, we can use the regular expression "[a-zA-Z0-9]+$.".
This will return False if the string contains any special characters other than Alphabets and Numbers; otherwise, True will be returned.
Example 1
In the example given below, we are taking a string as input and checking if it is alphanumeric using Regular Expressions −
import re str1 = "Tutorialspoint123" print("The given string is") print(str1) print("Checking if the given string is alphanumeric") print(bool(re.match('^[a-zA-Z0-9]+$', str1)))
Output
The output of the above example is as shown below −
The given string is Tutorialspoint123 Checking if the given string is alphanumeric True
Example 2
In the example given below, we are taking the same program as above but we are taking different input strings −
import re str1 = "1234@#$" print("The given string is") print(str1) print("Checking if the given string is alphanumeric") print(bool(re.match('^[a-zA-Z0-9]+$', str1)))
Output
The output of the above example is given below −
The given string is 1234@#$ Checking if the given string is alphanumeric False
- Related Articles
- How to write Python regular expression to check alphanumeric characters?
- How to check if a string is alphanumeric in Python?
- Java Regular expression to check if a string contains alphabet
- What is Raw String Notation in Python regular expression?
- Python - Check If All the Characters in a String Are Alphanumeric?
- Program to check regular expression pattern is matching with string or not in Python
- What is a regular expression in Python?
- Check if a character is alphanumeric in Arduino
- Python - Check if a variable is string
- Python program to check if the string is pangram
- What is the most elegant way to check if the string is empty in Python?
- Check if a string is Colindrome in Python
- What is regular expression in Java?
- Program to find whether a string is alphanumeric.
- Python program to check if the given string is pangram
