Python String isascii() Method

The isascii() method returns True if the string is empty or all characters in the string are ASCII.

ASCII stands for American Standard Code for Information Interchange. It is a character endcoding standard that uses numbers from 0 to 127 to represent English characters. For example, ASCII code for the character A is 65, and 90 is for Z. Similarly, ASCII code 97 is for a and 122 is for z. ASCII codes also used to represent characters such as tab, form feed, carriage return, and also some symbols. ASCII characters have code points in the range U+0000-U+007F.

Syntax:

str.isascii()

Parameters:

None.

Return Value:

Returns True if a string is ASCII, else returns False.

The following example demonstrates the isascii() method.

Example: isascii()
mystr = 'ABC'
print(mystr.isascii())

mystr = '012345'
print(mystr.isascii())

mystr = 'ABC'
print(mystr.isascii())

Output
True
True
True

An empty string, new line characterm symbols and numbers are also ASCII characters.

Example: isascii() with Numbers or Symbols
mystr = '#'
print(mystr.isascii())

mystr = '$'
print(mystr.isascii())

mystr = ''
print(mystr.isascii())

mystr = '\n'
print(mystr.isascii())
Output
True
True
True
True

The isascii() method also checks Unicode chars of ASCII, as shown below.

Example: isascii() with Unicode Chars of ASCII
mystr = '/u0000' # Unicode of Null
print(mystr.isascii())

mystr = '/u0041' # Unicode of A
print(mystr.isascii())

mystr = '/u0061'  # Unicode of a
print(mystr.isascii())

mystr = '/u007f' # Unicode of DEL
print(mystr.isascii())
Output
True
True
True
True

The isascii() method returns False for any non-ASCII characters.

Example: isascii() with Non-ASCII Chars
mystr = '/u00e2' # Unicode of â
print(mystr.isascii())

mystr = '/u00f8' # Unicode of ø
print(mystr.isascii())

mystr = 'õ'  
print(mystr.isascii())

mystr = 'Å' 
print(mystr.isascii())

mystr = 'ß'   # German letter
print(mystr.isascii())
Output
False
False
False
False
False
Want to check how much you know Python?