Skip to content
Python splitlines: preserve line endings for exact reassembly

Python splitlines: preserve line endings for exact reassembly

November 20, 2026

When editing text line by line, stripping terminators can lose both the newline style and whether the last line was terminated. splitlines(keepends=True) retains those distinctions in an existing string.

Set up

Tested on Linux with CPython 3.12.14. Use Python 3.12 or later, Bash, a text editor, mktemp, rm and rmdir. The example needs no network access. Run the steps in the same terminal and stop if setup fails. Run Python without -O so the assertions stay enabled.

start_dir="$PWD"
work_dir="$(mktemp -d)" || exit 1
cd -- "$work_dir" || exit 1

Run the example

Save this as preserve_line_endings.py:

text = "alpha\r\nbeta\ngamma\rdelta"
parts = text.splitlines(keepends=True)
print("preserved:", parts)
print("stripped:", text.splitlines())
print("reassembled:", "".join(parts) == text)
print("terminated:", "end\n".splitlines(keepends=True))
print("empty:", "".splitlines(keepends=True))
print("blank line:", "\n".splitlines(keepends=True))
assert "".join(parts) == text
assert parts[-1] == "delta"

Run:

python3 -B preserve_line_endings.py

Observed output:

preserved: ['alpha\r\n', 'beta\n', 'gamma\r', 'delta']
stripped: ['alpha', 'beta', 'gamma', 'delta']
reassembled: True
terminated: ['end\n']
empty: []
blank line: ['\n']

Keep each terminator with its line

The splitlines documentation covers several line boundaries, including carriage return, newline, and their two-character combination. Keeping endings allows an unchanged sequence of parts to reassemble into the original string.

A final newline stays attached to the last line; it does not create an additional empty item. An empty string produces no items, while a single newline represents a blank terminated line. These details differ from splitting on the literal newline character.

Limits

This preserves characters already present in a string. Default text-file reading can translate line endings before you call splitlines. If preserving original CR and LF characters matters, configure the text read with newline="" and a known encoding, or handle bytes separately. str.splitlines also recognizes Unicode line separators beyond CR and LF; it is not a parser for a protocol that permits only a specific delimiter.

Clean up

Remove the example file and the temporary directory:

rm -- preserve_line_endings.py
cd -- "$start_dir" || exit 1
rmdir -- "$work_dir"
unset start_dir work_dir

Acknowledgements

This post was created with assistance from AI.

Last updated on