

- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
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 check if a string is alphanumeric in Python?
Python String class has a method called isalnum() which can be called on a string and tells us if the string consists only of alphanumerics or not. You can call it in the following way:
>>> '123abc'.isalnum() True >>> '123#$%abc'.isalnum() False
You can also use regexes for the same result. For matching alpha numerics, we can call the re.match(regex, string) using the regex: "^[a-zA-Z0-9]+$". For example,
>>> bool(re.match('^[a-zA-Z0-9]+$', '123abc')) True >>> bool(re.match('^[a-zA-Z0-9]+$', '123#$%abc')) False
re.match returns an object, to check if it exists or not, we need to convert it to a boolean using bool().
- Related Questions & Answers
- What is the Python regular expression to check if a string is alphanumeric?
- Check if a character is alphanumeric in Arduino
- Python - Check if a variable is string
- How to check if a string in Python is in ASCII?
- Check if a string is Colindrome in Python
- How to check if a string is a valid keyword in Python?
- How to check if type of a variable is string in Python?
- Check if a string is Pangrammatic Lipogram in Python
- How to check if a character in a string is a letter in Python?
- Program to find whether a string is alphanumeric.
- How to check if a substring is contained in another string in Python
- How to check if a string is empty in Kotlin?
- Python - Check if a given string is binary string or not
- Check if a string is Isogram or not in Python
- Check if a string is suffix of another in Python
Advertisements