Python split() Method

Beginner
5 min read
Updated: Nov 2026
3 examples
String

Definition

split() cuts a string into pieces and returns them as a list of strings. With no argument it splits on runs of whitespace, so "learn python".split() is ['learn', 'python'], and it ignores spaces at both ends.

Pass a separator to split at that exact text instead. The separator is not kept, and two separators in a row give an empty string between them. The optional maxsplit limits how many cuts are made. The opposite job, putting the pieces back together, is done by join().

text.split() is a method of the built-in str type: call it on a string with dot notation. No import is needed, and it never changes the original string.

Syntax

Call it with no arguments to split on whitespace, or pass a separator and an optional limit.

python
parts = text.split()
parts = text.split(sep)
parts = text.split(sep, maxsplit)
CallWhat it does
"a b c".split()Returns ['a', 'b', 'c']. Any run of whitespace is one separator.
"a,b,c".split(",")Returns ['a', 'b', 'c']. The text is cut at every comma.
"a,b,c".split(",", 1)Returns ['a', 'b,c']. Only one cut is made, so the rest stays together.

To cut from the right use rsplit(). To split into lines use splitlines(). To cut once and keep the separator, use partition().

Library declarations
python
str.split(sep=None, maxsplit=-1) -> list
# returns: a list of strings
# raises: ValueError if sep is an empty string

Parameters

ParameterDescription
sepOptional. The text to split at. If it is omitted or None, the string is split on runs of whitespace. An empty string raises ValueError.
maxsplitOptional. The most cuts to make, so the list has at most maxsplit + 1 items. Default -1, which means no limit.

Return Value

Return ValueDescription
listA list of strings. The separator is not included in the pieces, and the original string is not changed.

Python Examples

Example 1 — Break a Sentence Into Words

Call split() with no arguments and use the list it returns.

python
sentence = "learn python the easy way"
words = sentence.split()

print(words)
print(len(words))
print(words[0])

How It Works

  1. No argument. split() cuts the sentence at each space and returns a list of five words.
  2. A normal list. len(words) counts the pieces, which gives 5.
  3. Use the pieces. words[0] is the first word, learn.

Example 2 — Empty Strings and Whitespace

See how a separator keeps empty pieces, and how split() without an argument does not.

python
row = "ada,,36,london"

print(row.split(","))
print("a  b".split(" "))
print("a  b".split())

How It Works

  1. An empty field. Two commas in a row have nothing between them, so the list gets an empty string.
  2. One space at a time. split(" ") cuts at each space, so two spaces give an empty string in the middle.
  3. No argument is smarter. split() treats the run of spaces as one separator and gives ['a', 'b'].

Example 3 — Split Only at the First Equals Sign

Use maxsplit to keep the rest of the text together, and check empty input.

python
line = "key=value=more"

print(line.split("="))
print(line.split("=", 1))

key, value = line.split("=", 1)
print(key)
print(value)
print("".split(","), "".split())

How It Works

  1. No limit. Without maxsplit the text is cut at both equals signs, giving three pieces.
  2. One cut. With maxsplit set to 1, only the first equals sign splits the text.
  3. Unpack the pair. The list always has two items here, so it can be unpacked into key and value.
  4. Empty input. "".split(",") gives a list with one empty string, while "".split() gives an empty list.

Frequently Asked Questions

It breaks a string into a list of substrings. With no argument it splits on whitespace. With a separator it splits at that exact text.
split() treats any run of whitespace as one separator and ignores spaces at the ends. split(" ") cuts at every single space, so extra spaces produce empty strings.
Pass maxsplit as 1, for example text.split("=", 1). The list then has at most two items. Use rsplit() to cut once from the right.
A separator that appears twice in a row, or at the start or the end, has nothing between the cuts, so an empty string is returned. Use split() with no argument, or filter the empty pieces.
Use the join() method on the separator, for example ",".join(parts).

Next: splitlines()

Break a string into a list of lines, at line breaks.

splitlines() tutorial →

CODETOFUN NEWS

Stay Ahead in Tech

Get the latest IT news, programming updates, and technology trends directly on your phone.

Your daily dose of tech, straight to your feed.

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

13 people found this page helpful