Skip to Content

5 Examples to Split String to List in Python

The split() method in Python splits strings into a list. It will return a list of the words in the string/line, separated by the delimiter string.

split string Syntax in Python 3

string.split(separator, max)

Parameter Description
separator The is a delimiter. The string splits at this specified separator. If is not provided then any white space is a separator.
maxsplit It is a number, which tells us to split the string into maximum of provided number of times. If it is not provided then there is no limit.
return The split() breaks the string at the separator and returns a list of strings.
  • If no separator is defined when you call upon the function, whitespace will be used by default.
  • In simpler terms, the separator is a defined character that will be placed between each variable.
  • The behavior of split on an empty string depends on the value of sep. If sep is not specified, or specified as None, the result will be an empty list.

split string into a list without separator in Python 3

  • without parameter – then space is used as separator
  • with parameter – comma, dot etc – see next section

print “Python2 Python3 Python Numpy”.split()
print “Python2, Python3, Python, Numpy”.split()

the result is:

[‘Python2’, ‘Python3’, ‘Python’, ‘Numpy’]
[‘Python2,’, ‘Python3,’, ‘Python,’, ‘Numpy’]

split string by separator in Python 3

Python split string by comma or any other character use the same method split() with parameter – comma, dot etc.

In the example below the string is split by comma and semi colon (which can be used for CSV files.

print “Python2, Python3, Python, Numpy”.split(‘,’)
print “Python2; Python3; Python; Numpy”.split(‘;’)
the result is:

[‘Python2’, ‘ Python3’, ‘ Python’, ‘ Numpy’]
[‘Python2’, ‘ Python3’, ‘ Python’, ‘ Numpy’]

Split multi-line string into a list (per line) in Python 3

We can use the same string method split and the special character for new line ‘n’. If the text contains some extra spaces we can remove them by strip() or lstrip():

str = “””
Python is cool
Python is easy
Python is mighty

list = []
for line in str.split(“n”):
if not line.strip():

print list
the result is:

[‘Python is cool’, ‘Python is easy’, ‘Python is mighty’]

split string by first occurrence in Python 3

If we need to do a split but only for several items and not all of them then we can use “maxsplit”. In this example we are splitting the first 3 comma separated items:

>>> str = “Python2, Python3, Python, Numpy, Python2, Python3, Python, Numpy”
>>> data = str.split(“, “,3)
>>> data
[‘Python2’, ‘Python3’, ‘Python’, ‘Numpy, Python2, Python3, Python, Numpy’]

Split string by consecutive separators(regex) in Python 3

If we want to split several consecutive separators as one(not like the default string split method) we need to use regex module in order to achieve it:

default split method vs module re:

import re

print(re.split(‘1+’, ‘Hello1111World’ ))
the result is:

[‘Hello’, ”, ”, ”, ‘World’]
[‘Hello’, ‘World’]
This is very useful when we want to skip several spaces or other characters.