Python 3.7+Strings (str)function
str.isascii
character checks
True if the string is empty or all characters are ASCII.
Syntax
s.isascii()
Added in Python 3.7, and unlike the other is-methods it returns True for the empty string. ASCII here means code points below 128, not printable: newline, tab and the null byte all pass. Handy as a cheap pre-check before encode('ascii'), or to catch a Cyrillic look-alike letter smuggled into an identifier.
Examples
print('abc'.isascii()) # → True
print('café'.isascii()) # → False
print(''.isascii()) # → True
See also
Open in the full glossarydocs.python.orgReport an inaccuracy