Python Strings: splitlines() Method

1. What is the splitlines() Method in Python?

The splitlines() method in Python is a built-in string function used to split a multiline string into a list of individual lines. It identifies line boundaries such as \n, \r, \r\n, and various Unicode line separators. This makes it extremely useful when you are working with multiline text, such as file contents, user-entered paragraphs, logs, or messages that include line breaks.
Python automatically handles different newline formats across operating systems, so you don’t need to worry about whether the text comes from Windows, Linux, or macOS.

2. Python splitlines() Method: Syntax, Parameters & Return Value

Python splitlines() Method Syntax


str.splitlines(keepends=False)

The syntax is simple and clean — just call the method on any string object.

Python splitlines() Method Parameters

Parameter Type Description
keepends boolean Optional. When set to True, newline characters are included at the end of each line. When False (default), newline characters are removed.

Python splitlines() Method Return Value

Return Value: List[str] — A list of strings obtained by splitting the original string at line boundaries (\n, \r, \r\n).
NOTE: The line break characters themselves are not included in the result by default.

3. How the Python splitlines() Method Works

The splitlines() function has predictable and easy-to-understand behavior:

  • It splits the string wherever a line boundary occurs (\n, \r, \r\n, and others).
  • Each line becomes a separate element in a list.
  • If keepends=False (default), Python removes the line endings.
  • If keepends=True, Python keeps the newline characters at the end of each line.
  • It is more reliable than manually using .split(“\n”) because it understands multiple newline formats automatically.
  • This makes splitlines() a convenient and platform-independent way to parse text files, logs, and multi-line user input.

    4. Examples of Python’s splitlines() Method

    Example 1: Basic usage with default keepends=False

    
    

    text = "Hello\nWorld\nWelcome" lines = text.splitlines() print(lines) # Output: ['Hello', 'World', 'Welcome']
    Explanation:

    Python splits the string at each newline (\n). Because keepends defaults to False, the newline characters are removed from the output list.

    Example 2: Using keepends=True to keep newline characters

    
    

    text = "Hello\nWorld\nWelcome" lines = text.splitlines(keepends=True) print(lines) # Output: ['Hello\n', 'World\n', 'Welcome']
    Explanation:

    With keepends=True, Python keeps the newline characters at the end of each line. The last element does not contain a newline since the original string did not end with one./p>

    Example 3: Handling different newline characters (\r, \r\n)

    
    

    text = "Line1\rLine2\r\nLine3\nLine4" lines = text.splitlines() print(lines) # Output: ['Line1', 'Line2', 'Line3', 'Line4']
    Explanation:

    The method automatically recognizes all major newline formats (\r, \r\n, \n) and splits the string correctly across operating systems.

    Example 4: Empty string returns an empty list

    
    

    text = "" lines = text.splitlines() print(lines) # Output: []
    Explanation:

    When the string is empty, splitlines() simply returns an empty list—there are no lines to extract.

    Example 5: String without newline returns a single-element list

    
    

    text = "Single line text" lines = text.splitlines() print(lines) # Output: ['Single line text']
    Explanation:

    If no newline characters exist, Python returns the entire string as one element in the list.

    Example 6: Multiline string with trailing newline and keepends=True

    
    

    text = "First line\nSecond line\n" lines = text.splitlines(keepends=True) print(lines) # Output: ['First line\n', 'Second line\n']
    Explanation:

    Because the string ends with a newline, both lines—including the final one—retain their newline characters.

    5. When to use splitlines() vs split(‘\n’)?

    Feature splitlines() split(‘\n’)
    Handles all newline types Yes (\n, \r, \r\n, others) No, only splits on \n
    Option to keep newline chars Yes, via keepends=True No native support
    Simplicity One method call Needs manual handling for all newline types

    6. Python splitlines() Method: Key Examples at a Glance

    Scenario Code Snippet Output Explanation
    Basic split (default) "a\nb\nc".splitlines() ['a', 'b', 'c'] Splits at newline characters without including them
    Keep newline characters "a\nb\nc".splitlines(True) ['a\n', 'b\n', 'c'] Preserves newline characters in each line
    Handle \r, \r\n "a\rb\r\nc".splitlines() ['a', 'b', 'c'] Recognizes and splits on different newline formats
    Empty string "".splitlines() [] Returns an empty list for empty input
    No newline "abc".splitlines() ['abc'] Returns the entire string as a single element

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top