The method str.isascii()
in Python is used to check if a string contains
str.isascii()
NA
: This method does not take any argument 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.Execute the code below and read the comments carefully.
# Check for ascii values# return Truestr1 = 'XYZ'print(str1, "has ASCII?:", str1.isascii())# Check for ascii values containing special characters# return Truestr2 = 'edpresso@educative.io'print(str2, "has ASCII?:", str2.isascii())# check for equivalent unicode to Asci# return Truestr3 = '/u0041' # Unicode of Aprint(str3, "has ASCII?:", str3.isascii())# Unicode value not having parallel ascii# return Falsestr4 = 'ß' # German letterprint(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
.