← Reference · Nestor G Pestelos Jr · Print this page

Programming Languages · Python

Python String Splitting

Reference entry · last updated September 20, 2026

Python string splitting is the set of str methods that divide a string into parts at a separator or a line boundary. The primary method is str.split, with str.rsplit splitting from the right, str.splitlines breaking at line boundaries, and str.partition returning the parts around one separator. These methods leave the original string unchanged.[1]

First principles and definitions

Strings and the split operation

A Python str is an immutable sequence of Unicode code points. Indexing and slicing read from it, and no method changes it in place. split, rsplit, and splitlines return lists of strings. partition and rpartition return tuples.[1]

str.split breaks a string into a list, and str.join combines strings from an iterable with a separator. Joining the parts may not recover the original: " ".join(text.split()) removes leading and trailing whitespace and replaces each internal run of whitespace with one space.

" ".join("  a  b ".split())   # 'a b'

Two splitting algorithms

The sep argument selects one of two algorithms. When sep is an explicit string, the method matches that substring and every match separates two parts. When sep is omitted or None, the method treats runs of consecutive whitespace as a single separator, which is a different rule set.[1]

The distinction changes the output shape. An explicit separator can produce empty strings, and the whitespace algorithm cannot produce them at the ends of the result.

Splitting methods

split

str.split(sep=None, maxsplit=-1) returns a list of the words in the string using sep as the delimiter. With an explicit separator, consecutive delimiters are not grouped and are treated as delimiting empty strings, so '1,,2'.split(',') returns ['1', '', '2']. The separator may be multiple characters and is treated as one delimiter. To split on several different delimiters, use re.split().[1][2]

"a,b,c".split(",")     # ['a', 'b', 'c']
"1,,2".split(",")      # ['1', '', '2']
"  a  b ".split()      # ['a', 'b']
"".split()             # []

rsplit

str.rsplit(sep=None, maxsplit=-1) behaves like split except that it splits from the right. When maxsplit is given, the rightmost splits are the ones taken. It is the common way to peel one suffix from a string.[1]

"a/b/c".rsplit("/", 1)   # ['a/b', 'c']

splitlines

str.splitlines(keepends=False) returns a list of the lines in the string, breaking at line boundaries. Line breaks are not included in the result unless keepends is true. The recognized boundaries are a superset of universal newlines and include \n, \r, and \r\n.[1]

"a\nb\r\nc".splitlines()      # ['a', 'b', 'c']
"a\nb".splitlines(True)       # ['a\n', 'b']

partition and rpartition

str.partition(sep) splits at the first occurrence of sep and returns a 3-tuple of the part before the separator, the separator itself, and the part after it. When the separator is not found, it returns the string followed by two empty strings. str.rpartition splits at the last occurrence. If the separator is absent, it returns two empty strings followed by the original string.[1] A 3-tuple with fixed positions is easier to unpack than a variable-length list when exactly one separator is expected.

"k=v".partition("=")    # ('k', '=', 'v')
"k".partition("=")      # ('k', '', '')
"k".rpartition("=")     # ('', '', 'k')

Separator and boundary rules

Explicit separator

With an explicit sep, each occurrence marks a boundary. Repeated delimiters delimit empty strings, and a delimiter at either end produces an empty string at that end. Splitting an empty string with an explicit separator also returns a one-element list containing the empty string.[1]

Whitespace separator

With sep omitted or None, runs of consecutive whitespace are treated as a single separator, and the result contains no empty strings at the start or end when the string has leading or trailing whitespace. Splitting an empty string or a string of only whitespace with a None separator returns [].[1]

This is why " a b ".split() returns two clean tokens while " a b ".split(" ") returns several empty strings.

maxsplit

When maxsplit is nonnegative, at most that many splits are performed, so the result has at most maxsplit + 1 elements. The default value -1 means no limit. split takes the leftmost splits and leaves the unsplit remainder in the final element. rsplit takes the rightmost splits and leaves the remainder in the first element.[1]

"a,b,c".split(",", 1)    # ['a', 'b,c']
"a,b,c".rsplit(",", 1)   # ['a,b', 'c']

Comparison

MethodReturnsBreaks atDirection
splitlist of stringsseparator matchesleft to right
rsplitlist of stringsseparator matchesright to left
splitlineslist of stringsline boundariesleft to right
partition3-tuplefirst separatorleft to right
rpartition3-tuplelast separatorright to left

The None separator applies only to split and rsplit. splitlines takes no separator, and partition requires one.

Choosing a method

References

  1. ^ Python Documentation, "Built-in Types," String Methods. Free full text: docs.python.org/3/library/stdtypes.html
  2. ^ Python Documentation, "re: Regular expression operations." Free full text: docs.python.org/3/library/re.html