How to check if a string in Python is in ASCII?


ASCII stands for American Standard Code for Information Interchange. ASCII is a character encoding system that assigns a unique number to each letter, digit, and symbol on a standard keyboard. In Python, each character has a numerical value based on the ASCII code, which can be checked using the ord() function. Understanding ASCII is important when working with text-based data and encoding or decoding text.

To check if a string in Python is in ASCII, you can use the built-in string module in Python.

Here are some ways you can check if a string is in ASCII:

Using the isascii() method

Example

In this example, the isascii() method checks if all characters in the string are ASCII characters. If all the characters in the string are ASCII, the method returns True, and the string is considered to be in ASCII.

my_string = "lorem ipsum"
if my_string.isascii():
    print("The string is in ASCII")
else:
    print("The string is not in ASCII")

Output

The string is in ASCII

Using a regular expression

Example

In this example, the regular expression ^[\x00-\x7F]+$ matches any string that contains only ASCII characters. If the regular expression matches the string, then the string is in ASCII.

import re
my_string = "lorem ipsum"

if re.match("^[\x00-\x7F]+$", my_string):
    print("The string is in ASCII")
else:
    print("The string is not in ASCII")

Output

The string is in ASCII

Using the ord() function

Example

In this example, the ord() function is used to get the ASCII value of each character in the string. If all the ASCII values are less than 128, then the string is in ASCII.

my_string = "lorem ipsum"
is_ascii = all(ord(c) < 128 for c in my_string)
if is_ascii:
    print("The string is in ASCII")
else:
    print("The string is not in ASCII")

Output

The string is in ASCII

You can use any of these methods to check if a string is in ASCII.

Here are two more code examples for checking if a string in Python is in ASCII:

Example

This code example uses the all() function and a list comprehension to check if all characters in the string have ASCII values less than 128. If all characters satisfy this condition, the string is considered to be in ASCII.

string1 = "lorem ipsum"

if all(ord(c) < 128 for c in string1):
    print("The string is in ASCII.")

else:
    print("The string is not in ASCII.")

Output

The string is in ASCII.

Example

This code example defines a function is_ascii() that checks if all characters in the string are printable ASCII characters. The function uses the string.printable constant, which contains all printable ASCII characters. If all characters in the string are printable ASCII characters, the function returns True and the string is considered to be in ASCII.

import string
def is_ascii(s):
    return all(c in string.printable for c in s)
string1 = "lorem ipsum"
if is_ascii(string1):
    print("The string is in ASCII.")
else:
    print("The string is not in ASCII.")

Output

The string is in ASCII.

Updated on: 10-Aug-2023

645 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements