Python splitlines() Method

Beginner
5 min read
Updated: Nov 2026
3 examples
String

Definition

splitlines() cuts a string at its line breaks and returns the lines as a list of strings. It understands the common endings: \n (Unix), \r\n (Windows), and a few rarer ones.

The line break characters are removed unless you pass keepends=True. A final newline does not create an extra empty line at the end, which is the main difference from split() with "\n". The original string is not changed.

text.splitlines() is a method of the built-in str type: call it on a string with dot notation. No import is needed, and it never changes the original string.

Syntax

Call it with no arguments, or pass True to keep the line breaks.

python
lines = text.splitlines()
lines = text.splitlines(keepends)
CallWhat it does
"a\nb\nc".splitlines()Returns ['a', 'b', 'c']. The text is cut at each newline.
"a\nb\n".splitlines()Returns ['a', 'b']. The final newline does not add an empty line.
"a\nb\n".splitlines(True)Returns ['a\n', 'b\n']. With True every line keeps its line break.

To split at a different character use split(). To put the lines back together use join(), and to clean each line use strip().

Library declarations
python
str.splitlines(keepends=False) -> list
# returns: a list of lines (strings)

Parameters

ParameterDescription
keependsOptional. If True, the line break characters stay at the end of each line. Default False.

Return Value

Return ValueDescription
listA list of strings, one for each line. The original string is not changed.

Python Examples

Example 1 — Mixed Line Endings

Split text that mixes \n and \r\n, and compare with split("\n").

python
text = "first line\nsecond line\r\nthird line\n"
lines = text.splitlines()

print(lines)
print(len(lines))
print(len(text.split("\n")))

How It Works

  1. Both endings work. splitlines() removes \n and \r\n alike, so no \r is left on the second line.
  2. Three lines. The final newline does not create a fourth, empty line.
  3. split("\n") disagrees. It cuts at the three \n characters, so it gives four items, and the last one is an empty string.

Example 2 — Use the keepends Option

Keep each line break, then join the lines back into the exact original text.

python
text = "alpha\nbeta\n"

print(text.splitlines())
print(text.splitlines(True))
print(text.splitlines(keepends=True) == text.splitlines(True))
print("".join(text.splitlines(True)) == text)

How It Works

  1. The default. The line breaks are removed from the lines.
  2. keepends. With True, each line ends with its \n.
  3. A keyword works too. keepends=True and the plain True give the same list.
  4. A round trip. Joining the lines with an empty separator rebuilds the original text exactly.

Example 3 — Print Numbered Lines

Loop over the lines with enumerate(), and see how an empty line is kept.

python
notes = "milk\n\neggs\nbread\n"

for number, line in enumerate(notes.splitlines(), 1):
    print(number, repr(line))

How It Works

  1. A list to loop over. splitlines() returns the lines, so a for loop can visit each one.
  2. Count from 1. enumerate(..., 1) starts the numbers at 1 instead of 0.
  3. The blank line stays. The empty line between milk and eggs is item 2, shown as an empty string.
  4. No extra line at the end. The final newline does not produce a fifth item.

Frequently Asked Questions

It splits a string at its line breaks and returns a list of the lines, without the line break characters unless keepends is True.
splitlines() also handles \r\n and other line breaks, and it does not add an empty string for a final newline. split("\n") cuts only at \n and does add that empty string.
Pass True, as in text.splitlines(True), or use the keyword keepends=True.
Yes. A blank line in the middle of the text becomes an empty string in the list. Only the final newline is not counted as an extra line.
Use "\n".join(lines). If you used keepends=True, use "".join(lines) instead.

Next: startswith()

Return True if the string starts with a given prefix, or any prefix in a tuple.

startswith() tutorial →

CODETOFUN NEWS

Stay Ahead in Tech

Get the latest IT news, programming updates, and technology trends directly on your phone.

Your daily dose of tech, straight to your feed.

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

14 people found this page helpful