Check String is ASCII in Python

  • Post category:String

In this article, you will see how to check string is ASCII in Python. An ASCII string is a string that contains only ASCII characters.

Step 1: Create a string

str = "¡¢"
print("str = ", str)
print("str type = ", type(str))

Step 2: Check if the string is ASCII

if all(ord(c) < 128 for c in str):
    print("ASCII")
else:
    print("Not ASCII")

Output:

str =  ¡¢
str type =  <class 'str'>
Not ASCII

Free resources to learn advanced skills: AiHints and CodeAllow