Private archive copy · Unofficial site · Content copyright belongs to Runoob Tutorial and the original authors · View Original

Python splitlines() method

Python splitlines() method

Python Strings Python Strings


Description

Python splitlines() splits at line boundaries (carriage return, carriage return plus newline, or newline) and returns a list with each line as an element. If keepends is False, line breaks are not included; if True, they are kept.

Syntax

Syntax of the splitlines() method:


str.splitlines([keepends])

Parameters

Return Value

Returns a list with each line as an element.

Example

The following example shows how to use the splitlines() function:

Example (Python 2.0+)

#!/usr/bin/python

str1

=

'

ab c

\n

\n

de fg

\r

kl

\r

\n

'

print

str1

.

splitlines

(

)

;

str2

=

'

ab c

\n

\n

de fg

\r

kl

\r

\n

'

print

str2

.

splitlines

(

True

)

The output of the above example is:


['ab c', '', 'de fg', 'kl']
['ab c\n', '\n', 'de fg\r', 'kl\r\n']

Python Strings Python Strings