str.splitlines() Split by line breaks
Last updated: 2026-09-22
str.splitlines() Split by line breaks
str.splitlines(keepends=False) splits on line boundaries.
| Category | String Methods |
|---|---|
| Kind | Built-in Type Method |
| Python Version | all |
📝 Syntax
str.splitlines(keepends=False)
⚙️ Parameters
| keepends Optional | Whether to keep the line break characters in the results.(Default:False) |
|---|
Returns:list[str]. The lines of the string, split at line boundaries.
Mutates the original:No (returns a new object)
▶ Example
Edit the code, press Run, and see the result in the output panel:
Output:
['a', 'b', 'c']
['a\n', 'b']
💡 Related Cases
More case studies coming soon — check back later.
❓ FAQ
QWhat is the difference between splitlines() and split('\n')?
Asplitlines() splits on all line breaks (\n, \r\n, \r, etc.) and does not produce a trailing empty string: 'a\nb\n'.splitlines() gives ['a', 'b'], whereas split('\n') gives ['a', 'b', ''].
QHow do I keep the line breaks?
APass keepends=True, and each line keeps its line break at the end.
QWhat does calling it on an empty string return?
AAn empty list [], without raising an error.