Skip to main content

Python String isascii() Function

The String isascii() method returns True if the string is not empty and all the characters are valid ASCII characters. Otherwise, it returns False.

Syntax

my_string.isascii()

isascii() Parameters

Python String isascii() function does not take any parameters.

isascii() Return Value

Python String isascii() function returns:

  • True if ALL characters in the string are ASCII characters.
  • False if AT LEAST ONE character is NOT ASCII character.

Examples

Example 1: Check if a String is ASCII with iascii()

The isascii() method returns True if all characters are ASCII characters.

For example, let's check if all characters in the string abcDEF are ASCII:

my_str = 'abcDEF'
result = my_str.isascii()
print(result) # Output: True

output

True

Example 2: Check if a String with Numbers is ASCII with isascii()

The isascii() method returns False if at least one character is not ASCII but numbers [0-9] are ASCII characters!

my_str = 'abc123'
result = my_str.isascii()
print(result) # Output: True

output

False

Example 3: Check if a String with Special Characters is ASCII with isascii()

The isascii() method returns False if at least one character is not ASCII. Symbols and control characters like newline \n, are considered ASCII characters.

my_str = 'abc@DEF'
result = my_str.isascii()
print(result) # Output: True

output

True

Other symbols that returns True:

my_str = '#'
print(my_str.isascii()) # Output: True

my_str = '$'
print(my_str.isascii()) # Output: True

my_str = ''
print(my_str.isascii()) # Output: True

my_str = '\n'
print(my_str.isascii()) # Output: True

output

True
True
True
True

But, if we try some non-ascii characters, iascii() method returns False:

my_str = '/u00e2' # Unicode of â
print(my_str.isascii()) # Output: False

my_str = '/u00f8' # Unicode of ø
print(my_str.isascii()) # Output: False

my_str = 'õ'
print(my_str.isascii()) # Output: False

my_str = 'Å'
print(my_str.isascii()) # Output: False

my_str = 'ß' # German letter
print(my_str.isascii()) # Output: False

output

False
False
False
False
False
warning

Pay attention to the ASCII standard characters!

Example 4: Check if an empty String is ASCII with isascii()

The isascii() method returns False if the string is empty.

my_str = ''
result = my_str.isascii()
print(result) # Output: False

output

False