Python bytes.isspace() Method

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

Definition

bytes.isspace() returns True if the object is not empty and every byte is one of the six ASCII whitespace bytes: space, \t, \n, \r, \x0b (vertical tab), and \x0c (form feed).

These are the same bytes that strip() and split() treat as whitespace by default. Bytes like \x00, \x1c, and \xa0 are not whitespace for bytes.

bytes.isspace() is a method of the built-in bytes type: call it on a bytes object with dot notation, such as data.isspace(...). No import is needed, and bytearray objects have the same method.

Syntax

No arguments; returns a bool.

python
result = data.isspace()
CallWhat it does
b" \t\n".isspace()Returns True.
b" x ".isspace()Returns False.
b"".isspace()Returns False.

A common blank-line test is not line.strip(), which is also True for empty lines.

Method signatures
python
bytes.isspace()
# returns True or False; bytearray.isspace() works the same way

Parameters

isspace() takes no parameters.

Return Value

Return ValueDescription
TrueAt least one byte, and all are ASCII whitespace.
FalseEmpty, or contains any other byte.

Python Examples

Example 1 — Skip Whitespace-Only Lines

Split data into lines, then keep only lines with real content. Empty lines are filtered by the truth test, whitespace-only lines by isspace().

python
data = b"a\n \t\n\r\nb\n"

lines = data.split(b"\n")
print(lines)
print([line for line in lines if line and not line.isspace()])

How It Works

  1. Raw lines. Splitting on LF leaves a space-tab line, a lone CR, and an empty last item.
  2. Two filters. line removes the empty item, and not line.isspace() removes lines made of whitespace.
  3. CR is whitespace. The leftover \r from a CRLF ending is dropped too.

Example 2 — Exactly Which Bytes Are Whitespace

Test all six whitespace bytes together, then three bytes that look like whitespace but are not.

python
for chunk in [b" \t\n\r\x0b\x0c", b"\x00", b"\xa0", b"\x1c", b""]:
    print(chunk, chunk.isspace())

How It Works

  1. The full set. All six ASCII whitespace bytes pass together.
  2. Null is not space. \x00 often pads binary data but is not whitespace.
  3. No-break space and separators. \xa0 and \x1c count as whitespace for str, but not for bytes.

Example 3 — The Non-Breaking Space as Text and as Bytes

Compare the str check with two different encodings of the same character.

python
nbsp = "\u00a0"

print(nbsp.isspace())
print(nbsp.encode("utf-8"), nbsp.encode("utf-8").isspace())
print(nbsp.encode("latin-1").isspace())

How It Works

  1. Unicode whitespace. As text, U+00A0 is whitespace.
  2. Two UTF-8 bytes. Encoded, it becomes \xc2\xa0, neither of which is ASCII whitespace.
  3. One Latin-1 byte. Even the single byte \xa0 fails, because bytes only know ASCII.

Frequently Asked Questions

Space, \t, \n, \r, \x0b, and \x0c.
No. b"\x00".isspace() is False.
The method requires at least one byte.
Not for bytes. Decode to str, where "\u00a0".isspace() is True.
Use not line.strip(), which is True for empty and whitespace-only lines.

Next: bytes.istitle()

True if the bytes are title-cased: each ASCII word starts uppercase and continues lowercase.

istitle() →

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.

13 people found this page helpful