Check If a String in Python is ASCII



ASCII stands for the American Standard Code for Information Interchange. It represents the characters using the integer value from 0 to 127.

In this article, we are going to explore the different ways to check if a string contains only ASCII characters in Python. It is necessary to verify whether a string contains only ASCII characters when ensuring compatibility.

Using Python isascii() Method

The Python isascii() method is a built-in method in Python 3.7. It returns true if all the characters in the string are ASCII; otherwise false.

Syntax

Following is the syntax for the Python isascii() method -

str.isascii()

Example 1

Let's look at the following example, where we are going to consider the characters "W, e, l, c, o, m, 1, 2" and check whether they are in the ASCII range or not.

str1 = "Welcome112"
result = str1.isascii()
if result:
   print("Given string is ASCII")
else:
   print("Given string is not ASCII")   

The output of the above program is as follows -

True

Example 2

In the following example, we are going to consider non-ASCII characters as input string -

str1 = "Welcomé"
print(str1.isascii())

The output of the above program is as follows -

False

Using Python ord() Function

The Python ord() Function accepts a character or a string of length "1" as a parameter and returns the Unicode value of the given character. Following is the syntax for the Python ord() function -

str.ord(x)

 We can verify whether a string value is ASCII using the ORD function. We need to pass each character of a string as a parameter to this method and verify if its value is less than 128 (As the ASCII value of the English alphabet is less than 128).

Example

Consider the following example, where we are going to use the expression with the all() to check if all the characters have a Unicode value less than 128.

str1 = "TutorialsPoint"
result = all(ord(x) < 128 for x in str1)
print(result) 

The following is the output of the above program -

True

Using Python "re" Module

The Python re module supports regular expressions, helping in string pattern matching and manipulation. It includes functions like match(), search() to identify, extract, or replace patterns in strings.

To verify that a string is ASCII, ie, the ASCII value of each character in the string is between 0 and 127. we need to match the desired string with the regular expression "^[\x00-\x7F]*$".

Example

Following is an example, where we are going to use the regular expression ^[\x00-\x7F]*$ to check every character in the string is between 0 and 127.

import re
str1 = "Welcome_to_TutorialsPoint"
reg_expression = r'^[\x00-\x7F]*$'
result = re.match(reg_expression, str1)
if result:
   print("Given String is ASCII")
else:
   print("Given String is not ASCII")

The output of the above program is as follows -

Given String is ASCII
Updated on: 2025-06-17T14:45:10+05:30

2K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started