Python 3.7+Byte sequencesfunction
bytearray.isascii
bytearray — is* checks
Check that every byte is ASCII (0–127); an empty bytearray also gives True.
Syntax
ba.isascii(...)
This is the one member of the is* family that returns True for an empty sequence: "every byte is within 0-127" holds vacuously when there are no bytes. It tests the value range only, not readability — control bytes such as NUL or ESC are ASCII as well. Available since Python 3.7.
Examples
print(bytearray(b'abc').isascii()) # → True
print(bytearray(b'abc\xff').isascii()) # → False
print(bytearray(b'\x7f').isascii()) # → True
print(bytearray(b'').isascii()) # → True
print(bytearray('привет'.encode('utf-8')).isascii()) # → False
See also
Open in the full glossarydocs.python.orgReport an inaccuracy