Python .splitlines()
.splitlines() is a built-in string method in Python method in Python that splits a multi-line string into a list of lines. It recognizes different newline characters such as \n, \r, or \r\n and splits the string at those points. The method returns a list of strings, each corresponding to a line in the original multi-line string.
Syntax
string.splitlines(keepends=False)
Parameters:
string: This is the string on which to apply the.splitlines()method.keepends(Optional): This is a Boolean parameter. IfTrue, the line break characters are included in the resulting lines. IfFalse(default), the line break characters are excluded.
Return value:
The .splitlines() method returns a list of strings, each corresponding to a line in the original multi-line string.
Example 1: Basic Usage of .splitlines()
In this example, .splitlines() is applied to a custom multi-line string with various line break characters (\n, \r\n, and \r):
custom_multi_line_string = "Line A\nLine B\r\nLine C\rLine D"custom_lines = custom_multi_line_string.splitlines()print(custom_lines)for line in custom_lines:print(line)
The output of this code is:
['Line A', 'Line B', 'Line C', 'Line D']Line ALine BLine CLine D
Example 2: Working with File Input
This example demonstrates how .splitlines() helps when reading entire file content as a string and needing to process it line by line:
with open("example.txt", "r") as file:content = file.read()lines = content.splitlines()print(lines)
The output of this code is (assuming file content is multi-line):
['This is line 1', 'This is line 2', 'This is line 3']
Codebyte Example: Keeping Line Endings
In this codebyte example, .splitlines(keepends=True) is used to include the line break characters in the resulting lines:
multi_line_string = "This is line 1.\nThis is line 2.\nThis is line 3."lines_with_breaks = multi_line_string.splitlines(keepends=True)print(lines_with_breaks)for line in lines_with_breaks:print(line)
Frequently Asked Questions
1. How is .splitlines() different from .split('\n')?
.split('\n') only handles \n as a newline, while .splitlines() handles all common line breaks (\n, \r, \r\n). This makes it more robust for cross-platform text handling.
2. Can .splitlines() be used on an empty string?
Yes. In that case, .splitlines() returns an empty list:
print("".splitlines()) # Output: []
3. What happens if there are trailing newlines while using .splitlines()?
Trailing newlines create extra empty strings only if keepends=True in .splitlines():
print("text\n".splitlines()) # Output: ['text']print("text\n".splitlines(True)) # Output: ['text\n']
Contribute to Docs
- Learn more about how to get involved.
- Edit this page on GitHub to fix an error or make an improvement.
- Submit feedback to let us know how we can improve Docs.
Learn Python on Codecademy
- Learn to analyze and visualize data using Python and statistics.
- Includes 8 Courses
- With Certificate
- Intermediate.13 hours
- Learn the basics of Python 3.12, one of the most powerful, versatile, and in-demand programming languages today.
- With Certificate
- Beginner Friendly.24 hours