Skip to content
String Methods

splitlines()

Split text at line boundaries.

str.splitlines(keepends=False)

str.splitlines() returns a list of lines in the string, breaking at line boundaries (\n, \r\n, \r).

Parameters

NameDescription
keependsIf True, keep each line ending in the returned pieces.

Returns

A list of lines.

Try it

Run it and change it
text = "first\nsecond"
print(text.splitlines())

Worth knowing

  • keepends option: Passing keepends=True preserves the trailing newline characters in each item of the returned list.

Related entries