Python 3 reference · String Methods
Python String splitlines() Method
Split the string into a list of lines at line boundaries (\n, \r\n and others), without the line endings.
Python 3 Since 3.0
Syntax
str.splitlines(keepends=False)
Parameters
| Parameter | Description |
|---|---|
keepends optional |
Keep the line-ending characters. Default: False |
Return value
A list of lines.
Examples
text = "first\nsecond\r\nthird"
print(text.splitlines())
print(text.splitlines(keepends=True))
Output
['first', 'second', 'third'] ['first\n', 'second\r\n', 'third']
print("a\nb\n".splitlines(), "a\nb\n".split("\n"))
Output
['a', 'b'] ['a', 'b', '']
Version history
| Version | Change |
|---|---|
| Python 3.0 | Added |
Related
str.split()· Split the string into a list of substrings at a separator. With no separator it splits on any whitespace.str.join()· Join the strings of an iterable into one string, with this string as the separator between them.- Python Strings · tutorial