What is string str.isascii() in Python?

Overview

The method str.isascii() in Python is used to check if a string contains ASCIIAmerican Standard Code for Information Interchange characters or not. It also checks for equivalent Unicode codes of ASCII values.

Syntax


str.isascii()

Parameters

NA: This method does not take any argument value.

Return value

It returns boolean values, either True or False.

  • True: if the string contains ASCII values.
  • False: if the string does not contain ASCII values.

Example

Execute the code below and read the comments carefully.

# Check for ascii values
# return True
str1 = 'XYZ'
print(str1, "has ASCII?:", str1.isascii())
# Check for ascii values containing special characters
# return True
str2 = 'edpresso@educative.io'
print(str2, "has ASCII?:", str2.isascii())
# check for equivalent unicode to Asci
# return True
str3 = '/u0041' # Unicode of A
print(str3, "has ASCII?:", str3.isascii())
# Unicode value not having parallel ascii
# return False
str4 = 'ß' # German letter
print(str4, "has ASCII?:", str4.isascii())

We called the isascii method for different highlighted strings in the code above. The isascii method will return True for the strings that only have ASCII characters. Otherwise, it will return False.

Free Resources