Python String isascii() Method
The Python string isascii() method is used to check whether all characters in a string are ASCII (American Standard Code for Information Interchange) characters. ASCII characters are those whose Unicode code points are in the range from 0 to 127.
When you call isascii() on a string, it returns True if all characters in the string are ASCII characters, and False otherwise. If the string is empty, the isascii() method returns True since there are no characters to check.
Syntax
Following is the basic syntax of the Python String isascii() method −
string.isascii()
Parameter
This method does not accept any parameters.
Return Value
The method returns a boolean value "True" or "False". It returns True if all characters in the string are ASCII characters, and False otherwise.
Example
In the following example, we are checking if all characters in the string "text" are ASCII characters using the isascii() method −
text = "Hello"
result = text.isascii()
print("The result is:",result)
Output
The output obtained is as follows −
The result is: True
Example
Here, we check if all characters in an empty string are ASCII characters using the isascii() method −
text = ""
result = text.isascii()
print("The result is:",result)
Output
Following is the output of the above code −
The result is: True
Example
In this example, we check if all the special characters such as "!", "@", "$" etc. in the string "text" are ASCII characters −
text = "!@#$%"
result = text.isascii()
print("The result is:",result)
Output
The result produced is as shown below −
The result is: True
Example
Now, we are checking if unicode characters in the string "text" are ASCII characters −
text = "Hèllo"
result = text.isascii()
print("The result is:",result)
Output
We get the output as shown below −
The result is: False
Example
This example checks if all characters in the string "text" are ASCII characters. Although the string is represented using Unicode escape sequences, the characters represented by the escape sequences are ASCII characters, so the result obtained is True −
text = "\u0048\u0065\u006c\u006c\u006f"
result = text.isascii()
print("The result is:",result)
Output
Following is the output of the above code −
The result is: True