This post will discuss how to split a delimited and non-delimited string into a list in Python.

Related Post:

Convert a string to a list of characters in Python

1. Using list() constructor

The list() constructor builds a list directly from an iterable, and since the string is iterable, you can construct a list from it by passing the string to the list constructor:

Download  Run Code

2. Using str.split() function

You can use the str.split(sep=None) function, which returns a list of the words in the string, using sep as the delimiter string.

For example, to split the string with delimiter -, you can do:

Download  Run Code

 
If sep is not specified or is None, consecutive whitespace runs are regarded as a single separator.

Download  Run Code

3. Using shlex.split() function

The shlex module defines the shlex.split(s) function, which split the string s using shell-like syntax.

Download  Run Code

That’s all about splitting a string into a list in Python.