ML Foundations

Python splitlines and mixed line endings

See how str.splitlines() recognizes CR, LF, and CRLF, while split('\\n') only separates at LF.

Example results for Python splitlines and mixed line endings. splitlines: 'first', 'second', 'third'
Illustration: Original illustration by Tensor Almanac
Evidence and testing

Review record: Tensor Almanac automated evidence gate (no independent human review). Example versions, recorded outputs and limitations appear below.

Use splitlines for logical lines

`str.splitlines()` recognizes line boundaries beyond LF, including CR and CRLF, and removes those separators from the returned strings by default. When a string mixes Windows-style CRLF, Unix LF, and classic Mac CR separators, one call gives the logical text lines without leaving a carriage return attached to some values. [1]

The method also treats CRLF as one boundary rather than two. This matters when parsing text already held in a Python string, such as a protocol sample or a fixture: a plain split on LF leaves the CR half of CRLF in the preceding element. [1, 2]

References: [1], [2]

Keep separators when the exact endings matter

Pass `keepends=True` when each returned line must retain the separator that ended it. The example shows the first line ending in `\r\n`, the second in `\n`, and the third in `\r`; those endings remain attached to their own lines. This is useful for inspecting or transforming mixed text while preserving its separators. [1]

If you only need to display or compare normalized logical lines, leave `keepends` at its default `False`. If you are reading a file rather than an in-memory string, remember that text-mode input may translate newline sequences before `splitlines()` ever sees them; this guide's fixture starts with an already-created string. [1, 2]

References: [1], [2]

Why split on LF is a different operation

`text.split("\\n")` uses exactly LF as its delimiter. It does not recognize a lone CR, and it leaves the CR from a CRLF pair in the preceding piece. It also returns an empty final element when the input ends with the delimiter, whereas `splitlines()` does not add an extra line for one terminal newline. [1, 2]

Choose `splitlines()` for text lines, especially when newline conventions may vary. Choose `split("\\n")` only when LF is the specific delimiter you want and the retained carriage return or trailing empty field is meaningful. The example prints both results so the difference is visible before choosing a parser. [1, 2]

Executed with Python 3.12.14; assertions passed.

mixed = "first\r\nsecond\nthird\r"

lines = mixed.splitlines()
kept = mixed.splitlines(keepends=True)
split_on_lf = mixed.split("\n")
trailing = "first\n"

print("splitlines:", lines)
print("keepends:", kept)
print("split LF:", split_on_lf)
print("terminal newline:", trailing.splitlines())
print("terminal LF split:", trailing.split("\n"))

assert lines == ["first", "second", "third"]
assert kept == ["first\r\n", "second\n", "third\r"]
assert split_on_lf == ["first\r", "second", "third\r"]
assert trailing.splitlines() == ["first"]
assert trailing.split("\n") == ["first", ""]

Recorded output

splitlines: ['first', 'second', 'third']
keepends: ['first\r\n', 'second\n', 'third\r']
split LF: ['first\r', 'second', 'third\r']
terminal newline: ['first']
terminal LF split: ['first', '']

References: [1], [2]

Sources

  1. Python string methods: splitlines
  2. Python string methods: split