Python Reference Library →

Python String splitlines() Method – Split String by Line Breaks

Let’s look at string splitlines() method – split string by line breaks

  1. Notice

    splitlines() splits at recognized line boundaries. By default the boundary characters are removed, and a final boundary does not add an empty last line.

  2. Try

    Follow the example, change its sample, and compare what happens.

  3. Check

    Explain the result and test your understanding.

Watch how it works

Focus on how the names, objects, and calculations change. We’ll inspect the printed output in the next section.

Explore the idea

Follow the code and watch its state change. Guided simulation.

Code
Python
text = 'tea\nmilk'
result = text.splitlines()
print(result)
Live state

Top-level scope

text→Object 1
result→Object 2
Object 1 · str'tea\nmilk'Object 2 · list['tea', 'milk']
This step

splitlines() splits at recognized line boundaries. By default the boundary characters are removed, and a final boundary does not add an empty last line.

Picture string splitlines() method – split string by line breaks

splitlines() splits at recognized line boundaries. By default the boundary characters are removed, and a final boundary does not add an empty last line.

Image brief

Alt text
A diagram for Python String splitlines() Method – Split String by Line Breaks. Instructions: text = 'tea\nmilk' result = text.splitlines() print(result). Displayed result: ['tea', 'milk']. The final example bindings are text --> object 1 (str); result --> object 2 (list). object 1: 'tea\nmilk'; object 2: ['tea', 'milk']
Image generation prompt
Create a clean educational illustration for a Python programming lesson titled "Python String splitlines() Method – Split String by Line Breaks". Teaching focus: splitlines() splits at recognized line boundaries. By default the boundary characters are removed, and a final boundary does not add an empty last line. Depict exactly the relationships and labels in this reference diagram: EXAMPLE INSTRUCTIONS text = 'tea\nmilk' result = text.splitlines() print(result) | v DISPLAYED RESULT ['tea', 'milk'] FINAL BINDINGS text --> object 1 (str) result --> object 2 (list) object 1: 'tea\nmilk' object 2: ['tea', 'milk'] Use a flat vector style, generous spacing, clear arrows and legible labels, with a restrained blue/teal palette on a plain background. The diagram must explain the programming idea accurately. Do not depict variables as boxes containing copied objects when they share a reference. Avoid mascots, decorative screenshots, logos, gradients and extra text. Landscape composition, approximately 3:2, suitable for a course card. Accessible description: A diagram for Python String splitlines() Method – Split String by Line Breaks. Instructions: text = 'tea\nmilk' result = text.splitlines() print(result). Displayed result: ['tea', 'milk']. The final example bindings are text --> object 1 (str); result --> object 2 (list). object 1: 'tea\nmilk'; object 2: ['tea', 'milk']

Follow the execution

Choose a different sample input, then follow every step through to the output. Before revealing it, predict what the program will display.

Execution workspace

Choose a sample and follow its execution. Guided simulation.

Code
Python
text = 'tea\nmilk'
result = text.splitlines()
print(result)
Live state

Top-level scope

text→Object 1
result→Object 2
Object 1 · str'tea\nmilk'Object 2 · list['tea', 'milk']
This step

The example has finished. Compare the final output with the values and references above.

Terminal
Output
['tea', 'milk']

What do you expect this program to print?

What does this example display?

text = 'tea\nmilk'
result = text.splitlines()
print(result)
What does this example display?
Choose one answer. You can try again.

Answer and explanation

['tea', 'milk']

Follow the printed values in order. The output is: ['tea', 'milk']

Which explanation matches what you learned?

Which explanation matches what you learned?
Choose one answer. You can try again.

Answer and explanation

splitlines() splits at recognized line boundaries. By default the boundary characters are removed, and a final boundary does not add an empty last line.

splitlines() splits at recognized line boundaries. By default the boundary characters are removed, and a final boundary does not add an empty last line.

More examples and reference

Python String splitlines() Method

The splitlines() method in Python is used to split a string into a list of lines. It's especially useful when you're working with multiline strings, files, or text blocks where line breaks matter.

Syntax

string.splitlines(keepends=False)

Parameters:

  • keepends (optional) – If True, line break characters (\n, \r, \r\n) are included in the output. Default is False.

Returns:

  • A list of lines in the string.

Example 1: Basic Usage

text = "Hello\nWorld\nPython"
lines = text.splitlines()
print(lines)
['Hello', 'World', 'Python']

Example 2: Keeping Line Breaks

text = "Hello\nWorld\nPython"
lines = text.splitlines(keepends=True)
print(lines)
['Hello\n', 'World\n', 'Python']

Use Case: Reading File Content

When reading file content into a single string, you can use splitlines() to process each line easily.

with open("file.txt", "r") as file:
    content = file.read()
    lines = content.splitlines()
    for line in lines:
        print(line)

Line Breaks Handled

splitlines() automatically handles:

  • \n – Unix/Linux line break
  • \r\n – Windows line break
  • \r – Old Mac line break

Common Mistakes

  • Using split('\n') instead of splitlines() will miss other newline types like \r and \r\n.
  • Forgetting to set keepends=True when line endings are important (e.g., in formatting).

Interview Tip

Use splitlines() in coding questions where you need to parse multi-line input efficiently. It’s cleaner than splitting manually with split('\n').

Summary

  • splitlines() splits a string into lines based on all common line break characters.
  • Returns a list of strings (lines).
  • Use keepends=True to retain line break characters.

Practice Problem

Write a program that reads a multi-line input from the user and prints each line with its line number.

text = """Python\nIs\nAwesome"""
for i, line in enumerate(text.splitlines(), 1):
    print(f"Line {i}: {line}")

What to remember

Connect what you noticed in the example with the idea below.

Remember

splitlines() splits at recognized line boundaries. By default the boundary characters are removed, and a final boundary does not add an empty last line.

Watch out for this idea

A final newline always creates an extra empty field in splitlines().

This is the misconception to avoid.

Reference checks are optional and do not count toward course progress.

    Reference library →