Python3 splitlines() method

Python3 字符串Python3 String


Description

Python splitlines() splits at line boundaries ('\r', '\r\n', '\n') and returns a list containing each line as an element. If the parameter keepends is False, the newline characters are not included; if True, they are kept.

Syntax

The syntax of the splitlines() method is:

str.splitlines([keepends])

Parameters

  • keepends -- whether to remove the newline characters ('\r', '\r\n', '\n') from the output. The default is False, which does not include newline characters; if True, newline characters are preserved.

Return Value

Returns a list containing each line as an element.

Examples

The following examples demonstrate the usage of the splitlines() function:

>>> 'ab c\n\nde fg\rkl\r\n'.splitlines()
['ab c', '', 'de fg', 'kl']
>>> 'ab c\n\nde fg\rkl\r\n'.splitlines(True)
['ab c\n', '\n', 'de fg\r', 'kl\r\n']
>>> 

Python3 字符串Python3 String

Other Extensions