Check a String is Alphanumeric in Python

  • Post category:String

In this article, you will see how to check a string is alphanumeric in Python. An alphanumeric string is a string that contains only letters and digits. The string module contains the isalnum() constants that contain all the letters and digits.

Step 1: Create a string

str = "CodeAllow123"
print("str = ", str)
print("str type = ", type(str))

Step 2: Check if a string is alphanumeric

if all(c.isalnum() for c in str):
    print("Alphanumeric")
else:
    print("Not Alphanumeric")

Output:

str =  CodeAllow123
str type =  <class 'str'>
Alphanumeric

Free resources to learn advanced skills: AiHints and CodeAllow