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).
Arguments
Parameters
| Name | Description |
|---|---|
| keepends | If True, keep each line ending in the returned pieces. |
Result
Returns
A list of lines.
Run it
Try it
Run it and change it
text = "first\nsecond" print(text.splitlines())
Output
Press Run to see what it prints.Worth knowing
- keepends option: Passing
keepends=Truepreserves the trailing newline characters in each item of the returned list.