Skip to Content

Python String Split – With Examples

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.

String split in python
s = "python is a fun programming language"
print(s.split())

Output:

['python', 'is', 'a', 'fun', 'programming', 'language']

  • Syntax
  • Examples

The following is the syntax to use the split() function:

str.split(sep, maxsplit)

Highlighted programs for you

Flatiron School

Flatiron School

Data Science Bootcamp
Product Design UX/UI Bootcamp

University of Maryland Global Campus

University of Maryland Global Campus

Cloud Computing Systems Master's
Digital Forensics & Cyber Investigation Master's

Creighton University

Creighton University

Health Informatics Master's

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.

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.


Subscribe to our newsletter for more informative guides and tutorials.
We do not spam and you can opt out any time.


Author

  • Piyush Raj

    Piyush is a data professional passionate about using data to understand things better and make informed decisions. He has experience working as a Data Scientist in the consulting domain and holds an engineering degree from IIT Roorkee. His hobbies include watching cricket, reading, and working on side projects.