Home / Python / Reference / String methods

Python string splitlines() method

Split text into a list of lines.

Syntax

str.splitlines(keepends=False)

Handles every kind of line ending (\n, \r\n) and does not leave an empty item at the end, unlike split("\n").

Parameters

keependskeep the line-break characters

Return value

list

Example

Code

text = "first\nsecond\r\nthird\n"
print(text.splitlines())
print(text.split("\n"))

Output

['first', 'second', 'third']
['first', 'second\r', 'third', '']

Related

Learn it step by step in the free lesson Files, with exercises that check your answers.