Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

iTechGuides is reader-supported. When you buy through links on our site, we may earn an affiliate commission. As an Amazon Associate I earn from qualifying purchases. Learn more

Use list(text) to get a list of a string’s sequence elements, or text.split(delimiter) to divide it wherever an exact delimiter occurs. For whitespace-separated words, call text.split() with no argument. If you need to split on alternative delimiter patterns, use re.split().

Turn a string into a list of elements

Pass the string to list() when you want one list item for each element in the string’s sequence:

text = "Python"
characters = list(text)
print(characters)  # ['P', 'y', 't', 'h', 'o', 'n']

Python strings are sequences, so this gives you a list of string elements. It does not always give one item for each character a person perceives: a visible character can be made from multiple Unicode code points. For grapheme-aware segmentation, list(text) is not a general-purpose solution.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Split on one exact delimiter

Give str.split() the delimiter you want to find. The delimiter can be more than one character; Python treats the entire string argument as one exact separator.

text = "red,green,blue"
fields = text.split(",")
print(fields)  # ['red', 'green', 'blue']

text = "1<>2<>3"
print(text.split("<>"))  # ['1', '2', '3']

With an explicit separator, consecutive delimiters produce empty fields rather than being collapsed:

print("1,,2".split(","))  # ['1', '', '2']
print("".split(","))      # ['']

This is useful when empty fields matter, but it may require validation if empty values are not allowed in your data. The Python 3.14.8 documentation explains that the sep argument can be a multi-character delimiter and directs users to re.split() for multiple delimiters: Python built-in types documentation.

Split on whitespace

Call split() without an argument to divide on runs of whitespace. Leading or trailing whitespace does not create empty fields, and repeated spaces or tabs count as one separator.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.
text = "red   greentblue"
print(text.split())  # ['red', 'green', 'blue']

print("   ".split())  # []

This differs from splitting on a literal space such as text.split(" "), which looks for that exact delimiter and can preserve empty fields between consecutive spaces.

Independent reader supportYour contribution helps us test, update, and keep practical guides available for everyone.Support on Ko-Fi

Choose a related method when the split behavior differs

Limit the number of splits

Use the optional maxsplit argument to limit how many times splitting occurs. The result contains at most maxsplit + 1 elements. Use rsplit() to apply that limit starting from the right.

text = "a,b,c,d"
print(text.split(",", 2))   # ['a', 'b', 'c,d']
print(text.rsplit(",", 2))  # ['a,b', 'c', 'd']

Split once and keep the delimiter

Use partition(sep) when you need to divide at the first occurrence of a separator and retain it in the result. It returns a three-item tuple: the part before the separator, the separator itself, and the part after it.

print("name=value".partition("="))  # ('name', '=', 'value')

Split on alternative patterns

Use re.split() from Python’s regular-expression module when separators can match different patterns, rather than one exact delimiter string. For example, a pattern can match either commas or semicolons:

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.
import re

print(re.split(r"[,;]", "red,green;blue"))  # ['red', 'green', 'blue']

Split text into lines

Use splitlines() for line boundaries. It is designed for line-oriented text and differs from split("n") in cases such as empty input and a terminal line break.

Quick choice

  • Need a list of the string’s sequence elements? Use list(text).
  • Need fields separated by one exact string? Use text.split(sep).
  • Need words separated by arbitrary whitespace runs? Use text.split().
  • Need alternative delimiter patterns? Use re.split().
  • Need to split once and keep the separator? Use partition().
  • Need to limit splits from the right or handle line boundaries? Use rsplit() or splitlines().

Product prices and availability are accurate as of the date/time indicated and are subject to change. Any price and availability information displayed on Amazon at the time of purchase will apply.