Split Line Function in Python: A Complete Guide

Formulas & functions · Updated November 22, 2026

The split() function in Python is a string method that divides a string into a list of substrings based on a specified delimiter, with the default delimiter being any whitespace. For example, "a b c".split() returns ['a', 'b', 'c']. This method is essential for parsing text data, reading files, and processing user input.

What Does split() Do Exactly?

Python strings are immutable sequences of characters. The split() method returns a new list object containing the substrings; it does not modify the original string. The method scans the string from left to right, using the delimiter to identify boundaries. When it finds a delimiter, it cuts the string and adds the segment to the list. Consecutive delimiters create empty strings in the result (unless you use the maxsplit parameter or the default whitespace handling).

Here is the basic syntax:

string.split(separator, maxsplit)

Both parameters are optional. If you omit separator, the method splits on any whitespace (spaces, tabs, newlines) and automatically removes leading/trailing whitespace, and treats consecutive whitespace as a single delimiter. If you provide a separator, it must be a string, and consecutive occurrences produce empty strings.

How to Use split() with a Delimiter

You can split on any character or substring. Common delimiters include commas, periods, pipes, or even multi-character strings. For instance:

data = "apple,banana,cherry"
print(data.split(','))  # ['apple', 'banana', 'cherry']

If you split on a comma and there is a trailing comma, you get an empty string at the end:

data = "a,b,"
print(data.split(','))  # ['a', 'b', '']

To split on a newline character, use split('\n'). However, if you are reading a file line by line, you might prefer read().splitlines() which handles different newline conventions.

Using maxsplit to Limit the Number of Splits

The maxsplit parameter controls how many splits are performed. If you set maxsplit=1, the string is split only at the first occurrence of the delimiter, returning a list with two elements. This is useful when you want to separate a key from the rest of a value, like parsing a header line.

line = "name=John Doe"
key, value = line.split('=', 1)
print(key, value)  # name John Doe

Without maxsplit, the method splits on every occurrence. With maxsplit, the remainder of the string after the last split is kept as the final element, including any delimiters.

Split on Whitespace: The Default Behavior

When you call split() with no arguments, it splits on any whitespace character (space, tab, newline, carriage return, form feed) and collapses multiple consecutive whitespace characters into one delimiter. It also strips leading and trailing whitespace. This is ideal for parsing simple text where words are separated by spaces.

text = "  Hello   world\n  Python  "
print(text.split())  # ['Hello', 'world', 'Python']

This behavior is different from split(' ') which treats each space as a separate delimiter and preserves empty strings. For example, "a b".split(' ') returns ['a', '', 'b'].

Common Problems and Misconceptions

One common mistake is confusing split() with splitlines(). splitlines() splits on line boundaries and handles \n, \r\n, etc., and does not include the newline characters in the result. Another issue is forgetting that split() returns a list of strings; if you need integers, you must convert each element with int() or use a list comprehension.

A frequent misconception is that split() can split on multiple different delimiters at once. It cannot; you must either use a regex with re.split() or chain multiple splits. Also, note that split() with an empty string as separator raises a ValueError; Python does not allow splitting on an empty string.

Alternatives to split()

For more complex splitting, Python’s re module provides re.split(), which accepts a regular expression pattern. This allows splitting on multiple delimiters or on patterns like digits or punctuation. For example:

import re
text = "apple;banana,cherry"
print(re.split(r'[;,]', text))  # ['apple', 'banana', 'cherry']

Another alternative is partition() and rpartition(), which split the string at the first (or last) occurrence of a separator and return a tuple of three parts: before, separator, after. This is useful when you need to keep the delimiter. For instance, "a=b".partition('=') returns ('a', '=', 'b').

Performance and Memory Considerations

For large strings, split() creates a new list and new string objects for each substring, which can be memory-intensive. If you only need to iterate over the parts, consider using re.finditer() or a generator-based approach. However, for most typical use cases, split() is fast enough. Python’s implementation of split() is optimized in C, so it is quite efficient for splitting on a single character. If you are splitting on whitespace, the default is also highly optimized.

If you are processing a file line by line, you can call split() on each line individually, which is memory-friendly because you only hold one line at a time. Avoid reading the entire file into a single string and then splitting if the file is huge.

Frequently Asked Questions

How do I split a string into characters?

To split a string into individual characters, you can use list(string) which returns a list of each character, or use a list comprehension [ch for ch in string]. The split() method cannot do this because it requires a delimiter; an empty string delimiter is not allowed.

Can I split a string and convert the parts to integers in one step?

No, split() always returns strings. You need to convert each part separately, for example using a list comprehension: [int(x) for x in "1 2 3".split()]. This is a common pattern when parsing numbers.

What is the difference between split() and splitlines()?

splitlines() splits on line boundaries (like \n, \r\n) and does not include the newline characters in the result. split() with no arguments splits on any whitespace, including spaces and tabs, and collapses multiple whitespace characters. If you want to split on newlines only, use splitlines().

How do I split a string on multiple delimiters?

Use re.split() with a character class, for example re.split(r'[,;]', text) splits on commas or semicolons. Alternatively, you can replace all delimiters with one common delimiter using str.replace() and then split, but this is less efficient for many delimiters.

Does split() modify the original string?

No, strings in Python are immutable. The split() method returns a new list and does not change the original string. If you need to keep the result, assign it to a variable.

Related

Try it: the Formula Explainer: paste any formula and see each function and reference in plain English.

More in Formulas & functions

Sources: Function behaviour and menu paths: Microsoft Support documentation for Excel. Method & sources

Spotted an error in this guide? Let us know.