Python bytes.splitlines() Method

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

Definition

bytes.splitlines() returns a list of lines. For bytes, the only line boundaries are \n, \r\n, and \r, so files with Unix, Windows, or old Mac line endings are all handled.

By default the line endings are removed; pass keepends=True to keep them. A final line ending does not create an extra empty line, which makes it a better fit for file content than split(b"\n").

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

Syntax

Optionally keep the line-ending bytes.

python
lines = data.splitlines()
lines = data.splitlines(keepends=True)
CallWhat it does
b"a\nb\r\nc".splitlines()Returns [b'a', b'b', b'c'].
b"a\n".splitlines(True)Returns [b'a\n'].
b"".splitlines()Returns [].

str.splitlines() also splits on characters such as \x0b, \x0c, and \x1c; the bytes version does not.

Method signatures
python
bytes.splitlines(keepends=False)
# line boundaries for bytes: \n, \r\n, \r

Parameters

ParameterDescription
keependsOptional. If true, each line keeps its line-ending bytes. Default False.

Return Value

Return ValueDescription
listA list of bytes objects, one per line.

Python Examples

Example 1 — Split Data with Mixed Line Endings

One buffer contains LF, CRLF, and CR endings. splitlines() handles all three, with or without keeping them.

python
data = b"one\ntwo\r\nthree\rfour"

print(data.splitlines())
print(data.splitlines(keepends=True))

How It Works

  1. Three styles. \n, \r\n, and \r each end a line.
  2. CRLF is one boundary. The two bytes \r\n produce one break, not an empty line between them.
  3. Keep the endings. With keepends=True each line keeps exactly the bytes that ended it; the last line had none.

Example 2 — Bytes Ignore Other Line Breaks

Vertical tab and form feed are line breaks for str, but not for bytes. Compare both on the same data.

python
raw = b"a\x0bb\x0cc\nd"

print(raw.splitlines())
print(raw.decode("ascii").splitlines())

How It Works

  1. Only LF splits. In bytes mode, \x0b and \x0c are ordinary bytes inside the first line.
  2. str is broader. After decoding, str.splitlines() treats vertical tab and form feed as line breaks too.
  3. Choose deliberately. Use bytes for protocol data where only CR and LF matter, and str for general text.

Example 3 — splitlines() vs split(b"\n") at the End

Most text files end with a newline. Compare how the two methods treat it, and check two edge cases.

python
log = b"start\nrunning\ndone\n"

print(log.splitlines())
print(log.split(b"\n"))
print(b"".splitlines(), len(b"\n\n".splitlines()))

How It Works

  1. No phantom line. splitlines() returns exactly the three lines.
  2. Extra empty item. split() on b"\n" adds b'' after the final newline.
  3. Edge cases. Empty data has no lines, and two newlines mean two empty lines.

Frequently Asked Questions

Only \n, \r\n, and \r.
Pass keepends=True: data.splitlines(keepends=True).
splitlines() does not create a line after a final line ending. split(b"\n") does.
No. Those are line boundaries only for str.splitlines().
Iterate over the file object, or call data.splitlines() on the bytes you read.

Next: bytes.startswith()

Check whether bytes begin with a prefix or any prefix from a tuple.

startswith() →

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.

14 people found this page helpful