Python 3.7+Byte sequencesfunction
bytes.isascii
bytes — is* checks
Checks that every byte is ASCII (0–127); an empty sequence also gives True.
Syntax
b.isascii(...)
ASCII here means only the 0-127 range, not 'printable text': a null byte, a newline and other control codes all pass. The practical value is the guarantee — a True result means decode('ascii') cannot fail, and any ASCII-compatible encoding yields the same characters. Added in Python 3.7.
Examples
print(b'abc'.isascii()) # → True
print(b'Hi 123!\n'.isascii()) # → True
print(b''.isascii()) # → True
print(b'\x80'.isascii()) # → False
print('привет'.encode().isascii()) # → False
See also
Open in the full glossarydocs.python.orgReport an inaccuracy