Skip to the content.

Split String with Delimiter

We can split a string with delimiter(s) using re (Regular Expression) module.

Example

>>> import re
>>> text = "These are collections of words seperated by a space."
>>> print(re.split(" ", text))
['These', 'are', 'collections', 'of', 'words', 'seperated', 'by', 'a', 'space.']

Here, we’re splitting the string text using " " (space) delimiter.

Also, for customs delimiters we can do something like -

re.split(" |\n")

Source: W3Resource