ID EN
Built-in Types

splitlines

Python 3.11

Return a list of the lines in the string, breaking at line boundaries. Line breaks are not included in the resulting list unless keepends is given and true.

Syntax

PYTHON
str.splitlines(keepends=False)

Examples

Example 1
PYTHON
>>> 'ab c\n\nde fg\rkl\r\n'.splitlines()
['ab c', '', 'de fg', 'kl']
>>> 'ab c\n\nde fg\rkl\r\n'.splitlines(keepends=True)
['ab c\n', '\n', 'de fg\r', 'kl\r\n']
Example 2
PYTHON
>>> "".splitlines()
[]
>>> "One line\n".splitlines()
['One line']
Example 3
PYTHON
>>> ''.split('\n')
['']
>>> 'Two lines\n'.split('\n')
['Two lines', '']

See Also

universal newlines split()