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() method does not take any parameters.
isascii() Return Value
Python String isascii() method returns:
Trueif ALL characters in the string are ASCII characters.Falseif 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: Trueoutput
TrueExample 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: Trueoutput
FalseExample 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: Trueoutput
TrueOther 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: Trueoutput
True
True
True
TrueBut, 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: Falseoutput
False
False
False
False
FalseExample 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: Falseoutput
False