6 demos of Python split string (split, rsplit and splitlines)

How to use split string method?

The Python split method is used to break a given string by the specified delimiter like a comma.

The method returns a list of words that are broken from the specified separator (delimiter string). For example:

string_to_break.split (‘,’)

The above string will break into words by using comma as separator.

comma split example splitlines example max splits rsplit example

(See the last section of this tutorial for Python splitlines method)

Syntax of split method

The general syntax of using the split method is:

str.split(sep, maxsplit)

Where, sep is the separator like a comma, space or multiple characters. If not provided, space will be taken as a delimiter.

The other parameter specifies the maximum number of splits. If this parameter is not provided, the split method will break the string at the maximum number possible.

See the examples in the following section for using the split method simply and with different parameters. I  have also covered other split methods like rsplit and splitlines at the last part, so keep reading.

An example of splitting a string without specifying any parameter

In this example, a string is created. After that, the split method is used to break the string and returned list is displayed:

Python split

See online demo and code

The code:

 

The output of the above code is the broken string with four words, as such, the string was broken with space as delimiter.

[‘The’, ‘string’, ‘split’, ‘method’]

An example of using comma as separator

In this example, a comma is used as a separator in the split Python method. No maximum parameter is given, so string should break fully:

string split

See online demo and code

The code:

 

You can see, the returned list consists of three pieces:

[‘The string split’, ‘ a Python method’, ‘ for breaking strings’]

An example of specifying maximum splits

Using the same string as in above example, however, the maxsplit parameter is used as 1. See the code and output by clicking the link or image below:

split maximum

See online demo and code

The code for using the maximum parameter:

 

and the output is:

[‘The string split’, ‘ a Python method, for breaking strings’]

You see, it returned a list of two items only, that is, the maximum value + 1.

Using for loop with split string method

As such, the split method returns a list of words, you may use the for loop to iterate through the list items after breaking the string. See the following example:

split for loop

See online demo and code

The code with for loop:

 

You can see, the string is broken into three pieces and a for loop is used to display those items for the returned list.

An example of rsplit method

In an above example, where I specified the maxsplit parameter, the first split was from the first comma from the left side. If you want to break from the right side of the given string, use the rsplit Python method.

To make it clearer, see the example below where a string is split by using both split and rsplit methods with maxsplit=1. Both lists are displayed after executing the methods to see the difference:

rsplit

See online demo and code

The code for the split and rsplit:

 

 

The output:

List after split method:  [‘The string split’, ‘ a Python method, for breaking strings’]

List after rsplit method:  [‘The string split, a Python method’, ‘ for breaking strings’]

You can see, the rsplit started breaking the string from the right side.

An example of using splitlines method

The Python splitlines method splits the given string at the line boundaries. The supported boundaries are the subset of universal newlines. For example,

  • \n for line feed
  • \r means carriage return
  • \x1c for file separator

See the supported list here.

By using the splitlines method in a string, it returns a list of lines broken from these boundaries. Note that, it does not include line breaks in a string.

See an example below, where a string is created with “\n” twice:

str_tst = “Python is a powerful language. \n It has many string functions. \n Split is one of those!”

splitlines

See online demo and code

This is how the splitlines method is used in above example:

 

The output:

[‘Python is a powerful language. ‘, ‘ It has many string functions. ‘, ‘ Split is one of those!’]