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.
SyntaxFollowing is the basic syntax of the Python String isascii() method −
string.isascii()Parameter
This method does not accept any parameters.
Return ValueThe method returns a boolean value "True" or "False". It returns True if all characters in the string are ASCII characters, and False otherwise.
ExampleIn 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: TrueExample
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: TrueExample
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: TrueExample
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: FalseExample
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
boolean_string_methods.htm
RetroSearch is an open source project built by @garambo | Open a GitHub Issue
Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo
HTML:
3.2
| Encoding:
UTF-8
| Version:
0.7.4