In python, a string can be split into smaller chunks using the split()
function. split()
is a string function that returns a list of words in the string separated by the delimiter string passed. By default, any whitespace is a delimiter.

s = "python is a fun programming language" print(s.split())
Output:
['python', 'is', 'a', 'fun', 'programming', 'language']
Table of Contents
- Syntax
- Examples
Syntax
The following is the syntax to use the split()
function:
str.split(sep, maxsplit)
Highlighted programs for you
Flatiron School
Flatiron School
University of Maryland Global Campus
University of Maryland Global Campus
Creighton University
Creighton University
Parameters:
- sep (optional): The separator to use for splitting the string. It’s an optional parameter and if not provided, any whitespace in python is a separator.
- maxsplit: (optional): Specifies the maximum number of splits to be made. Its default value is -1, signifying no limit to the splits.
Returns:
A list of strings resulting from splitting the given string by the separator passed.
Note: There are a number of characters in python that are regarded as whitespace characters. If a separator is not passed, the string is split on the occurrence of any of these whitespace characters. The following is a list of such characters in python:
' '
– Space'\t'
– Horizontal tab'\n'
– Newline'\v'
– Vertical tab'\f'
– Feed'\r'
– Carriage return
You can use the string function isspace()
to check if all the characters in a string are whitespace characters or not.
Examples
Example 1: Using the default separator.
s = "python is a fun programming\nlanguage" print(s.split())
Output:
['python', 'is', 'a', 'fun', 'programming', 'language']
In the above example, since a separator is not provided, the default separator (any whitespace character) is used. We can see that the string was split not only at the spaces ' '
but also at the newline character '\n'
since it’s also a whitespace character.
Example 2: Using a custom separator.
s = "python-is-a-fun-programming language" print(s.split("-"))
Output:
['python', 'is', 'a', 'fun', 'programming language']
In the above example, we use dash '-'
as our separator. And we can that the string has been split at all the occurrences of '-'
. Since there is no '-'
between 'programming'
and 'language'
they have not been split.
Example 3: Specifying a maxsplit
s = "python is a fun programming language" print(s.split(maxsplit=1))
Output:
['python', 'is a fun programming language']
In the above example, we specified the maximum number of splits to be 1
. Hence, the string was split only once starting from left.
For more on python’s split()
function, refer to the python docs.
Other articles on python strings:
Subscribe to our newsletter for more informative guides and tutorials.
We do not spam and you can opt out any time.