Python check if a string characters are ASCII using isascii

Python string isascii method example:

isascii method was introduced in Python 3.7. Using this method, we can check if a string contains all ASCII characters. This method can be used to check all characters are ASCII or not without iterating through the characters one by one and test them.

In this post, I will show you how to use isascii with example.

Definition of isascii():

isascii is defined as below:

str.isascii()

This method returns a boolean value. It is True if the string is empty or all characters in the string are ASCII. Else it returns False.

Example of isascii:

Let’s take a look at the below example:

str_arr = ['a', '', 'abc', 'ab♥c123', '123♥', 'abc#$%', '♥♥♥♥♥']

for item in str_arr:
    print('{} isascii : {}'.format(item, item.isascii()))

Here, we are checking isascii for all of the strings in str_arr. It gives the below output:

a isascii : True
 isascii : True
abc isascii : True
ab♥c123 isascii : False
123♥ isascii : False
abc#$% isascii : True
♥♥♥♥♥ isascii : False

python isascii example

You might also like: