CKTIS / Python
str.splitlines
Splits text at recognized line boundaries, optionally retaining line endings.
Version: Python 3.14; repository image, production runtime not attested
Syntax
s.splitlines(keepends=False)Splits text at recognized line boundaries, optionally retaining line endings.
A final line ending terminates the last line without creating an extra empty line.
Carriage return followed by line feed is recognized as one line boundary. The terminating line feed closes the last line without adding an empty one. keepends=True retains its newline inside the corresponding returned string.
assert "a\r\nb\n".splitlines(True) == ["a\r\n", "b\n"] and "".splitlines() == []
Examples
print("one\r\ntwo\n".splitlines())
print("one\n".splitlines(keepends=True))
Result
['one', 'two'] ['one\n']
Carriage return followed by line feed is recognized as one line boundary. The terminating line feed closes the last line without adding an empty one. keepends=True retains its newline inside the corresponding returned string.