Skip to content
Home » Splitting Strings in Python

Splitting Strings in Python

Spread the love

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

splitting strings

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.']

Your Panda3D Magazine

Make Awesome Games and Other 3D Apps

with Panda3D and Blender using Python.

Cool stuff, easy to follow articles.

Get the magazine here (PDF).

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.']

Python Jumpstart Course

Learn the basics of Python, including OOP.

with lots of exercises, easy to follow

The course is available on Udemy.

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

Blender Jumpstart Course

Learn the basics of 3D modeling in Blender.

step-by-step, easy to follow, visually rich

The course is available on Udemy and on Skillshare.

Here’s the video version of the article:


Spread the love

Leave a Reply