Python 3 reference · String Methods

Python String splitlines() Method

Split the string into a list of lines at line boundaries (\n, \r\n and others), without the line endings.

Python 3 Since 3.0

Syntax

str.splitlines(keepends=False)

Parameters

ParameterDescription
keepends optional Keep the line-ending characters. Default: False

Return value

A list of lines.

Examples

Split into linesTry it yourself
text = "first\nsecond\r\nthird"
print(text.splitlines())
print(text.splitlines(keepends=True))

Output

['first', 'second', 'third']
['first\n', 'second\r\n', 'third']
No empty last item, unlike split('\\n')Try it yourself
print("a\nb\n".splitlines(), "a\nb\n".split("\n"))

Output

['a', 'b'] ['a', 'b', '']

Version history

VersionChange
Python 3.0Added