Check if a string is in ASCII in Python

avatar

Borislav Hadzhiev

Last updated: Sep 18, 2022

banner

Photo from Unsplash

Check if a string is in ASCII in Python #

Use the str.isascii() method to check if a string is in ASCII, e.g. if my_str.isascii():. The str.isascii() method returns True if the string is empty or all characters in the string are ASCII, otherwise False is returned.

main.py
my_str = 'bobbyhadz.com' if my_str.isascii(): # ๐Ÿ‘‡๏ธ this runs print('The string contains only ASCII characters') else: print('The string does NOT contain only ASCII characters') print('bobby'.isascii()) # ๐Ÿ‘‰๏ธ True print('bobby hadz'.isascii()) # ๐Ÿ‘‰๏ธ True print(''.isascii()) # ๐Ÿ‘‰๏ธ True print('ab ั„ะณ'.isascii()) # ๐Ÿ‘‰๏ธ False

We used the str.isascii() method to check if a string only contains ASCII characters.

The str.isascii method returns True if the string is empty or all characters in the string are ASCII, otherwise False is returned.

main
print(''.isascii()) # ๐Ÿ‘‰๏ธ True print('BOBBY'.isascii()) # ๐Ÿ‘‰๏ธ True print('BOBBY HADZ'.isascii()) # ๐Ÿ‘‰๏ธ True print('BOBBY_HADZ!.?'.isascii()) # ๐Ÿ‘‰๏ธ True print('ะคะคะค'.isascii()) # ๐Ÿ‘‰๏ธ False

ASCII characters have code points in the range U+0000-U+007F.

If you consider empty strings non-ASCII, check for the string's length.

main.py
my_str = '' if my_str.isascii() and len(my_str) > 0: print('The string contains only ASCII characters') else: # ๐Ÿ‘‡๏ธ this runs print('The string does NOT contain only ASCII characters')

Alternatively, you can use a try/except statement.

Check if a string is in ASCII using try/except #

To check if a string is in ASCII:

  1. Use the str.encode() method to encode the string with the ASCII encoding.
  2. Catch potential UnicodeEncodeError exceptions in an except block.
  3. If the str.encode() method runs successfully, the string is in ASCII.
main.py
def is_ascii(string): try: string.encode('ascii') except UnicodeEncodeError: return False else: return True print(is_ascii('bobby')) # ๐Ÿ‘‰๏ธ True print(is_ascii('bobby hadz')) # ๐Ÿ‘‰๏ธ True print(is_ascii('')) # ๐Ÿ‘‰๏ธ True print(is_ascii('ab ั„ะณ')) # ๐Ÿ‘‰๏ธ False

We used a try/except/else statement to check if a string contains only ASCII characters.

The try statement uses the str.encode() method to encode the string into bytes with the ASCII encoding.

The str.encode method returns an encoded version of the string as a bytes object. The default encoding is utf-8.

If the string cannot be encoded into bytes with the ASCII encoding, a UnicodeEncodeError is raised and is handled in the except block.

If the str.encode() method runs successfully, no error is raised and the else block runs.

Alternatively, you can use the all() function.

Check if a string is in ASCII using all() #

To check if a string is in ASCII:

  1. Use a generator expression to iterate over the string.
  2. Check if the Unicode code point of each character is less than 128.
  3. If the condition is met for all characters, the string is in ASCII.
main.py
def is_ascii(string): return all(ord(char) < 128 for char in string) print(is_ascii('bobby')) # ๐Ÿ‘‰๏ธ True print(is_ascii('bobby hadz')) # ๐Ÿ‘‰๏ธ True print(is_ascii('')) # ๐Ÿ‘‰๏ธ True print(is_ascii('ab ั„ะณ')) # ๐Ÿ‘‰๏ธ False

We used a generator expression to iterate over the string.

Generator expressions are used to perform some operation for every element or select a subset of elements that meet a condition.

On each iteration, we use the ord() function to check if the Unicode code point of the current character is less than 128.

The ord function takes a string that represents 1 Unicode character and returns an integer representing the Unicode code point of the given character.

main.py
print(ord('a')) # ๐Ÿ‘‰๏ธ 97 print(ord('b')) # ๐Ÿ‘‰๏ธ 98

The standard ASCII characters are in the range 0-127, so we check if each character has a Unicode code point of less than 128.

The all() built-in function takes an iterable as an argument and returns True if all elements in the iterable are truthy (or the iterable is empty).

main.py
def is_ascii(string): return all(ord(char) < 128 for char in string) print(is_ascii('bobby')) # ๐Ÿ‘‰๏ธ True print(is_ascii('bobby hadz')) # ๐Ÿ‘‰๏ธ True print(is_ascii('')) # ๐Ÿ‘‰๏ธ True print(is_ascii('ab ั„ะณ')) # ๐Ÿ‘‰๏ธ False

If all characters in the string are in ASCII, the function returns True, otherwise False is returned.

I wrote a book in which I share everything I know about how to become a better, more efficient programmer.
book cover
You can use the search field on my Home Page to filter through all of my articles.

Copyright ยฉ 2022 Borislav Hadzhiev