str.splitlines()
Added in v3.x · Updated March 13, 2026 · String Methods
stdlib string methods
The splitlines() method breaks a string into a list of lines. It handles different line ending characters automatically—including Unix (\n), Windows (\r\n), and old Mac (\r) line endings—making it more convenient than manually splitting on \n.
Signature
str.splitlines(keepends=False)
Parameters
| Parameter | Type | Default | Description |
|---|---|---|---|
keepends | bool | False | If True, line endings are included in the resulting strings. |
Return Value
Returns a list of lines. The line endings themselves are not included in the substrings unless keepends is set to True.
Basic Examples
# Simple multiline string
text = "first line\nsecond line\nthird line"
text.splitlines()
# Result: ['first line', 'second line', 'third line']
# Works with different line endings
"line1\r\nline2\rline3".splitlines()
# Result: ['line1', 'line2', 'line3']
# Empty lines are preserved
"line1\n\nline3".splitlines()
# Result: ['line1', '', 'line3']
The keepends Parameter
When keepends=True, the line endings are preserved in the resulting strings:
# Keep line endings
"line1\nline2".splitlines(keepends=True)
# Result: ['line1\n', 'line2']
# Useful for reconstructing with different line endings
lines = "one\ntwo\nthree".splitlines(keepends=True)
"".join(lines)
# Result: 'one\ntwo\nthree'
# With Windows-style line endings
"a\r\nb\r\nc".splitlines(keepends=True)
# Result: ['a\r\n', 'b\r\n', 'c']
How It Differs from split('\n')
Using splitlines() is generally preferred over split('\n') because it handles all line ending styles correctly:
# Using split('\n') - doesn't handle all cases
"line1\r\nline2".split('\n')
# Result: ['line1\r', 'line2'] -遗留 \r!
# Using splitlines() - handles it properly
"line1\r\nline2".splitlines()
# Result: ['line1', 'line2']
# Another edge case: trailing newline
"text\n".split('\n')
# Result: ['text', ''] - includes empty string at end
"text\n".splitlines()
# Result: ['text'] - clean result
Common Use Cases
Reading file contents
# When you read a file into a string
content = "line one\nline two\nline three"
lines = content.splitlines()
# Process each line
for line in lines:
print(f"Processing: {line}")
Processing user input or pasted text
# Text pasted from a form or clipboard
user_text = "apple\r\nbanana\r\ncherry"
fruits = user_text.splitlines()
# fruits = ['apple', 'banana', 'cherry']
Splitting output from commands
# Simulating command output
output = "file1.txt\nfile2.txt\nfile3.txt"
files = output.splitlines()
# files = ['file1.txt', 'file2.txt', 'file3.txt']
Preserving structure with keepends
# Transform line endings while preserving structure
original = "line1\r\nline2\r\nline3"
lines = original.splitlines(keepends=True)
# Convert to Unix-style line endings
new_text = "".join(lines).replace("\r\n", "\n")
# new_text = 'line1\nline2\nline3'
Practical Notes
splitlines()recognizes\n(Unix),\r\n(Windows),\r(old Mac),\v(vertical tab),\f(form feed), and\x85(next line) as line boundaries- A string ending with a line ending will not produce an extra empty string at the end
- For simple use cases where you know the line ending,
split('\n')works—butsplitlines()is safer and more readable