Python bytes.isascii() Method

Beginner
5 min read
Updated: Nov 2026
3 examples
bytes method

Definition

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()
CallWhat 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 ValueDescription
TrueEvery byte is 0-127, or the object is empty.
FalseAt 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())

How It Works

  1. Plain text. Every byte of hello is below 128.
  2. Empty is ASCII. No byte breaks the rule, so empty bytes return True.
  3. UTF-8 detected. \xc3 and \xa9 are above 127.
  4. Range edges. 0 and 127 are both inside ASCII, even though they are not printable.

Example 2 — Pick a Codec Before Decoding

Decode pure ASCII data with the ASCII codec, and fall back to UTF-8 with replacement for anything else.

python
def to_text(data):
    if data.isascii():
        return data.decode("ascii")
    return data.decode("utf-8", errors="replace")


print(ascii(to_text(b"plain")))
print(ascii(to_text("na\u00efve".encode("utf-8"))))
print(ascii(to_text(b"bad\xff")))

How It Works

  1. Fast path. ASCII bytes decode safely with the ASCII codec.
  2. Valid UTF-8. The encoded word decodes back to the letter U+00EF.
  3. 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())

How It Works

  1. Euro sign. The UTF-8 encoding of the euro sign is \xe2\x82\xac, so the check fails.
  2. First offender. A generator with next() returns the index of the first byte above 127, here 8.
  3. 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.

Next: bytes.isdigit()

True if all bytes are ASCII digits 0-9 and there is at least one byte.

isdigit() →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

10 people found this page helpful