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.

s = "python is a fun programming language"


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

  • Syntax
  • Examples

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

str.split(sep, maxsplit)


  • 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.


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"


['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"


['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"


['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.

In this tutorial, we covered how to split strings in python using the split() function. If you found this article useful do give it a share! For more such articles subscribe to us.

If you’re a beginner looking to start your data science journey and learn python, check out our Python for Data Science Series.