Python Function Reference

str.splitlines

Split text into lines at line breaks.

Python 3.13
str.splitlines(keepends=False)

Parameters

keependsbooloptional
If True, the line break characters stay at the end of the parts.

Return value

A list of strings; an empty list for an empty string.

Examples

Different line breaks

Python 3.13
print('one\ntwo\r\nthree'.splitlines())
['one', 'two', 'three']

Difference from split by \n

Python 3.13
text = 'a\nb\n'
print(text.split('\n'))
print(text.splitlines())
['a', 'b', '']
['a', 'b']

Details

It understands \n, \r\n and other line breaks. Unlike split("\n"), it gives no empty string at the end if the text ends with a line break.

See also