Python splitlines() method
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
- keepends -- whether to keep line breaks ('\r', '\r\n', \n') in the output. Defaults to False, which excludes line breaks; if True, line breaks are kept.
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
'
str1
.
splitlines
(
)
;
str2
=
'
ab c
\n
\n
de fg
\r
kl
\r
\n
'
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