Python String | splitlines method
Start your free 7-days trial now!
Python's str.splitlines(~)
method splits the string at line breaks, and returns a list containing the separated lines.
The representations in the list below are considered line breaks:
Representation | Description |
---|---|
| Line Feed |
| Carriage Return |
| Carriage Return + Line Feed |
| Line Tabulation |
| Form Feed |
| File Separator |
| Group Separator |
| Record Separator |
| Next Line (C1 Control Code) |
| Line Separator |
| Paragraph Separator |
Parameters
1. keepends
| boolean
| optional
Whether the line breaks themselves are included in the returned list. By default, False
(i.e. not included).
Return value
A list of separated lines.
Examples
Basic usage
To split the string "abc\n e f\rg"
at line breaks:
x = "abc\n e f\rg"x.splitlines()
['abc', ' e f', 'g']
We can see that the string is split twice into three parts (once at \n
and once at \r
).
Keepends parameter
To split the string "abc\n e f\rg"
at line breaks and include the line breaks in the returned list:
y = "abc\n e f\rg"y.splitlines(True)
['abc\n', ' e f\r', 'g']
We can see that the returned list includes the line breaks \n
and \r
.