
- Python Basic Tutorial
- Python - Home
- Python - Overview
- Python - Environment Setup
- Python - Basic Syntax
- Python - Comments
- Python - Variables
- Python - Data Types
- Python - Operators
- Python - Decision Making
- Python - Loops
- Python - Numbers
- Python - Strings
- Python - Lists
- Python - Tuples
- Python - Dictionary
- Python - Date & Time
- Python - Functions
- Python - Modules
- Python - Files I/O
- Python - Exceptions
Python - Check If All the Characters in a String Are Alphanumeric?
To check if all the characters in a string are alphanumeric, we can use the isalnum() method in Python and Regex also. First, let us understand what is alphanumeric.
What is Alphanumeric?
Alphanumeric includes both letters and numbers i.e., alphabet letter (a-z) and numbers (0-9). Examples: A, a, k, K, 8, 10, 20, etc.
Let us see an example of an Alphanumeric string
8k9q2i3u4t
Let us see an example of a string wherein all the characters aren’t Alphanumeric -
$$###k9q2i3u4t
Check If All the Characters in a String Are Alphanumeric using isalnum()
We will use the built-in isalnum() method to check if all the characters are alphanumeric -
# String1 mystr1 = "8k9q2i3u4t" # Display the string2 print("String = ",mystr1); # Check string1 for alphanumeric print("Is String1 alphanumeric = ",mystr1.isalnum()) # String2 mystr2 = "##$$9hnkh67" # Display the string2 print("\nString2 = ",mystr2); # Check string2 for alphanumeric print("Is String2 alphanumeric = ",mystr2.isalnum())
Output
String = 8k9q2i3u4t Is String1 alphanumeric = True String2 = ##$$9hnkh67 Is String2 alphanumeric = False
Check If All the Characters in a String Are Alphanumeric using Regex
For using Regex in Python, you need to install the re module. To install, use pip
pip install re
To use the re module, import it:
import re
Let us now see an example
import re # String1 mystr1 = "m4w5r6" # Display the string2 print("String = ",mystr1); # Check string1 for alphanumeric print("Is String1 alphanumeic = ",bool(re.match('^[a-zA-Z0-9]+$', mystr1))) # String2 mystr2 = "pn#$nk2h1" # Display the string2 print("\nString2 = ",mystr2); # Check string2 for alphanumeric print("Is String2 alphanumeic = ",bool(re.match('^[a-zA-Z0-9]+$', mystr2)))
Output
String = m4w5r6 Is String1 alphanumeic = True String2 = pn#$nk2h1 Is String2 alphanumeic = False
- Related Articles
- Python - Check if frequencies of all characters of a string are different
- How to check if a string is alphanumeric in Python?
- Python program to check if a string contains all unique characters
- How to remove all non-alphanumeric characters from a string in MySQL?
- What is the Python regular expression to check if a string is alphanumeric?
- Check if the characters of a given string are in alphabetical order in Python
- Check whether the frequencies of all the characters in a string are prime or not in Python
- How to write Python regular expression to check alphanumeric characters?
- Check if a string has all characters with same frequency with one variation allowed in Python
- Check if all the 1s in a binary string are equidistant or not in Python
- How to check if a string only contains certain characters in Python?
- Check if a character is alphanumeric in Arduino
- How to check if a unicode string contains only numeric characters in Python?
- Check if the characters in a string form a Palindrome in O(1) extra space in Python
- Check if string contains special characters in Swift

Advertisements