3 Examples to Split String into List in Python 3

Table of Contents

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.

python 3 string split Syntax

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.
  • If sep is specified as any string, the result will be a list containing one element which is an empty string .

Python 3 split string into a list without separator

  • 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’]

Python 3 split string into a list by separator

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’]

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

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():
continue
list.append(line.lstrip())

print list
the result is:

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

Python 3 split string by first occurrence

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’]

Python 3 Split string by consecutive separators(regex)

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(‘Hello1111World’.split(‘1’))
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.

Table of Contents

Share on facebook
Facebook
Share on twitter
Twitter
Share on linkedin
LinkedIn

You might also like