In one of my previous articles we were talking about joining strings. If you want to read it, here it is. In this article we’ll be talking about splitting strings. We’ll discuss two methods here: split and splitlines. Let’s start with the former:
Splitting Strings with the split Method
The method split([sep, maxsplit]) splits a string into a list of substrings using the sep delimiter character as the position to split. The delimiter is not included in the result. If no delimiter is given, it defaults to whitespace. The optional parameter maxsplit is used to limit the number of splits:
>>> text = "This text is so long. Why not have a list of shorter ones?"
If there are no arguments, the whole string is split on the whitespaces:
>>> text.split()
['This', 'text', 'is', 'so', 'long.', 'Why', 'not', 'have', 'a', 'list', 'of', 'shorter', 'ones?']
Now let’s split on the period:
>>> text.split(".")
['This text is so long', ' Why not have a list of shorter ones?']
This time let’s split on each lowercase ‘t’:
>>> text.split("t")
['This ', 'ex', ' is so long. Why no', ' have a lis', ' of shor', 'er ones?']
Next let’s split on the whitespaces, but let’s limit the number of splits to 3. So, we get 3 substrings split on the delimiter and another substring containing the rest of the string:
>>> text.split(" ", 3)
['This', 'text', 'is', 'so long. Why not have a list of shorter ones?']
Now let’s limit the number of splits to 1:
>>> text.split(" ", 1)
['This', 'text is so long. Why not have a list of shorter ones?']
The splitlines Method
The method splitlines([num]) splits a string on the newlines. If the optional parameter num is True, the line breaks are also included:
>>> text = "Here we have\n some newline \ncharacters just to \nsee how it works."
Should there be no arguments, the line breaks are not included:
>>> text.splitlines()
['Here we have', ' some newline ', 'characters just to ', 'see how it works.']
If there is an argument that evaluates to True, the line breaks are included:
>>> text.splitlines(1)
['Here we have\n', ' some newline \n', 'characters just to \n', 'see how it works.']
>>> text.splitlines(True)
['Here we have\n', ' some newline \n', 'characters just to \n', 'see how it works.']
The line breaks are not included if the argument evaluates to False:
>>> text.splitlines(0)
['Here we have', ' some newline ', 'characters just to ', 'see how it works.']
It also works for multiline strings:
>>> poem = """
... There was a young lady of Lynn
... who was so exceedingly thin
... that when she essayed
... to drink lemonade
... she suddenly slipped through the straw
... and fell in
... """
>>> poem.splitlines()
['', 'There was a young lady of Lynn', 'who was so exceedingly thin', 'that when she essayed', 'to drink lemonade', 'she suddenly slipped through the straw', 'and fell in']
>>> poem.splitlines(True)
['\n', 'There was a young lady of Lynn\n', 'who was so exceedingly thin\n', 'that when she essayed\n', 'to drink lemonade\n', 'she suddenly slipped through the straw\n', 'and fell in\n']
Here’s the video version of the article: