Python split() Function: How to Split Strings with Examples

Formulas & functions · Updated November 22, 2026

Use Python’s string method split() to turn one string into a list of smaller strings. Call text.split() to split on runs of whitespace, or pass a separator such as a comma with text.split(','). You can also limit the number of splits with maxsplit. The examples below show how to choose the right form, inspect the result, and avoid common edge cases.

What You’ll Need

  • Python 3 installed, or an environment where you can run Python code, such as a notebook or an interactive Python shell.
  • A string you want to divide. You can use a literal in an example or a value stored in a variable.
  • Basic familiarity with variables, strings, lists, and calling a method with parentheses. No extra package is needed for the built-in split() method.

Step-by-Step

  1. Put the text in a string. The split() method belongs to string objects, so start with a string variable. For example, message = 'Python makes text processing easier' stores a line of text in message. You can also call the method directly on a string literal, but a variable is often easier to reuse and inspect.
  2. Split on whitespace when the text is separated by spaces. Call message.split() with no arguments. Python returns a list containing ['Python', 'makes', 'text', 'processing', 'easier']. With no separator specified, Python treats consecutive whitespace characters as one boundary and does not include whitespace at the start or end of the string in the result. This makes the no-argument form useful for turning an ordinary sentence into words.
  3. Pass a delimiter when the text uses a specific character. If a string contains comma-separated values, give the comma as the separator: colors = 'red,green,blue', then colors.split(',') returns ['red', 'green', 'blue']. The separator is removed from the resulting pieces. Use the exact delimiter that appears in the data; a comma, semicolon, pipe, or multi-character marker can be passed as a string.
  4. Check whether spaces around separated values need cleaning. An explicit separator splits only at that exact sequence, so 'red, green, blue'.split(',') returns ['red', ' green', ' blue'], with a leading space in the last two items. If that input is expected, remove surrounding whitespace from each item with a list comprehension: [item.strip() for item in colors.split(',')]. The result is ['red', 'green', 'blue']. Do not add this cleanup automatically if spaces are meaningful in your data.
  5. Limit how many times the string is split. The optional second argument, maxsplit, sets the maximum number of splits, not the maximum number of returned items. For example, 'one two three four'.split(' ', 2) returns ['one', 'two', 'three four']. There are two split points and three resulting pieces; the unprocessed remainder stays together in the final item. This is useful when the last field may contain spaces.
  6. Use rsplit() to split from the right. The related string method rsplit() uses the same basic arguments but applies a limited split starting at the end. For example, 'report.2026.final.csv'.rsplit('.', 1) returns ['report.2026.final', 'csv']. This can help separate a final suffix when earlier parts may contain the same delimiter. If there is no limit, split() and rsplit() produce the same pieces in the same order.
  7. Inspect and use the returned list. Store the result if you need it again, such as parts = message.split(). You can access an item by index, for example parts[0], loop over the pieces with for part in parts:, or check the number of pieces with len(parts). Remember that Python list indexes start at zero, so the first item is at index 0. If the input might be empty or have an unexpected number of fields, validate it before accessing a particular index.

What the arguments mean: the general form is string.split(sep=None, maxsplit=-1). Leaving out sep, or setting it to None, selects Python’s whitespace behavior. A nonnegative maxsplit limits the number of splits; the default value, -1, means there is no limit. The original string is not changed: split() returns a new list.

Common Mistakes

Using a literal space when whitespace varies. text.split(' ') treats each ordinary space as an exact separator. If the text has two spaces together, the output contains an empty string between them; tabs and line breaks are not treated as that same separator. By contrast, text.split() groups runs of whitespace and is often the better choice for sentences or loosely formatted text. Choose based on whether repeated separators should create empty fields.

Expecting an explicit separator to be trimmed. When you use split(','), spaces next to commas stay attached to the pieces. Use strip() on each item if the spaces are unwanted. Similarly, split() does not remove punctuation from words: splitting a sentence on whitespace can leave a comma or period at the end of a word.

Forgetting that repeated delimiters can make empty items. For example, 'a,,b'.split(',') returns ['a', '', 'b']. A leading or trailing comma also creates an empty first or last item. This behavior can be important when empty fields represent missing data, so do not discard them without considering what they mean.

Passing an empty separator. text.split('') raises a ValueError; an empty string is not a valid delimiter. If you need individual characters, use list(text). If you need groups of characters according to a pattern, consider a regular expression instead.

Assuming split() understands quoted fields. A basic split does not parse CSV rules. For example, splitting 'Ada,"London, UK",34' on commas breaks the quoted city into two pieces. For real CSV files or quoted comma-separated records, use Python’s csv module, which handles quoting and escaping, rather than trying to fix the result with more splits.

Tips

  • Pick the separator based on the source format. Use no argument for word-like separation by whitespace, and an explicit separator for structured text that consistently uses a delimiter. A separator can be more than one character: record.split('::') splits at each exact double-colon sequence.
  • Use splitlines() for lines of text. If you have a whole block of text and want its lines, text.splitlines() is clearer than splitting on a single newline character. It recognizes common line boundaries and, by default, does not add an extra empty item just because the text ends with a line break. Use splitlines(keepends=True) when you want line-ending characters retained.
  • Unpack fields only when their count is predictable. If entry.split(':') always produces exactly three items, you can assign them to three variables. If the input is not controlled, first check the length or catch an appropriate error; otherwise, unpacking may fail when there are too few or too many pieces. A limited split can be useful when the final field may itself contain the delimiter.
  • Rejoin pieces when you need a string again. Use the separator’s join() method, such as ', '.join(items), to combine a list of strings with comma-space separators. Splitting and joining are not always exact reverses: whitespace can be normalized, and separators removed by a split are only restored if you deliberately add them back.
  • Use a regular expression only for pattern-based boundaries. The standard re.split() function can split on patterns such as one or more commas or semicolons. It is more flexible than str.split(), but for a single fixed delimiter, the string method is simpler and easier to read.

Troubleshooting

The result has unexpected empty strings. Print the original value with repr(text) so spaces, tabs, and line breaks are easier to spot. If you used a specific separator, check for repeated, leading, or trailing copies of that delimiter. If those empty fields are not meaningful, decide explicitly whether to filter them out; filtering can change the relationship between positions and source fields.

The split returns only one item. Confirm that the separator actually occurs in the string and matches exactly, including capitalization and spaces. For instance, splitting on ', ' will not match a comma with no following space. If the input uses inconsistent whitespace, use split() for whitespace separation or normalize the text before using a fixed separator.

You get an index or unpacking error afterward. The split may have returned fewer pieces than you expected. Check the list itself and its length before reading an index or assigning items to multiple variables. For input that can vary, handle missing fields deliberately instead of assuming every record has the same shape.

The output is wrong for a file or complex record. Inspect a few raw input lines, including quoted values and escaped characters. A simple delimiter split is appropriate only when the format’s rules are simple. Use a parser designed for the format, such as csv for CSV data, when delimiters can appear inside quoted values or the format has additional rules.

You need to split on several different delimiters. The built-in method accepts one exact separator at a time. For a small, well-defined input, you can replace one delimiter with another before splitting, but be sure that replacement will not alter legitimate content. For more varied patterns, use re.split() and test it on examples that include the edge cases in your data.

Frequently Asked Questions

What does split() return in Python?
It returns a list of strings. The original string remains unchanged, and the separator is not included in the pieces unless you add it back separately.

What is the difference between split() and split(' ')?
With no argument, split() groups runs of whitespace and ignores whitespace at the ends. With a literal space, only that exact character is a separator; repeated spaces can therefore produce empty strings, and tabs are not treated as spaces.

How do I split only once?
Use text.split(separator, 1) to split once from the left, or text.rsplit(separator, 1) to split once from the right. Each call returns at most two items.

Can I split a string into individual characters?
Not with an empty separator; that raises ValueError. Use list(text) to get a list of its characters. For some Unicode text, a visible character can consist of multiple code points, so a specialized Unicode-aware approach may be needed if you need user-perceived characters.

How do I keep the delimiter when splitting?
str.split() removes the delimiter. If you need to preserve matched delimiters as separate pieces, use an appropriate pattern with re.split() and a capturing group, or retain the original string and its delimiter positions for later processing.

Should I use split() to read CSV data?
Not when fields can contain quoted commas, quotes, or other CSV rules. Use Python’s built-in csv module so fields are parsed according to the format rather than divided at every comma.

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.