bytes.isascii() returns True if every byte value is from 0 to 127, the ASCII range. Unlike the other checks, it returns True for empty bytes.
Control bytes such as \x00 and \x7f are ASCII too, so this is not a "printable" test. It is a fast way to decide whether data can be decoded with the ASCII codec or needs UTF-8 handling.
bytes.isascii() is a method of the built-in bytes type: call it on a bytes object with dot notation, such as data.isascii(...). No import is needed, and bytearray objects have the same method. New in Python 3.7.
Syntax
No arguments; returns a bool (Python 3.7+).
python
result = data.isascii()
Call
What it does
b"hello".isascii()
Returns True.
b"".isascii()
Returns True.
b"\x80".isascii()
Returns False.
ASCII data is also valid UTF-8, so isascii() being True guarantees both decodes succeed.
Method signatures
python
bytes.isascii()
# returns True or False; bytearray.isascii() works the same way
Parameters
isascii() takes no parameters.
Return Value
Return Value
Description
True
Every byte is 0-127, or the object is empty.
False
At least one byte is 128 or higher.
Python Examples
Example 1 — Which Byte Strings Are ASCII?
Test plain text, empty bytes, UTF-8 text, and two control bytes at the edges of the ASCII range.
python
for data in [b"hello", b"", b"caf\xc3\xa9", b"\x00\x7f"]:
print(data, data.isascii())
Fast path. ASCII bytes decode safely with the ASCII codec.
Valid UTF-8. The encoded word decodes back to the letter U+00EF.
Invalid byte.\xff is never valid UTF-8, so decode() with errors="replace" inserts U+FFFD.
Example 3 — Locate the First Non-ASCII Byte
When a check fails, find where the problem starts so you can report it.
python
data = b"price: 5\xe2\x82\xac"
print(data.isascii())
pos = next(i for i, b in enumerate(data) if b > 127)
print(pos, hex(data[pos]))
print(data[:pos].isascii())
Output
False
8 0xe2
True
How It Works
Euro sign. The UTF-8 encoding of the euro sign is \xe2\x82\xac, so the check fails.
First offender. A generator with next() returns the index of the first byte above 127, here 8.
Clean prefix. Everything before that index is ASCII.
Frequently Asked Questions
Python 3.7.
There is no byte outside the ASCII range, so the condition holds. This differs from the other is...() methods.
No. Control bytes like \x00 and \n are ASCII too.
Loop with enumerate() and look for values above 127.
Yes. Every ASCII byte decodes to the same character in UTF-8.