Python Split String (split, rsplit and splitlines)

Infographic: Python Split, rsplit and splitlines Methods - Visual guide explaining the Python split methods, illustrating how it splits a string into substrings based on a specified delimiter and its optional arguments.

How to use the Python split string method?

  • Splitting a string is a common operation in Python (or other programming languages).
  • In Python, splitting of a string can be done by using various methods such as split(), rsplit(), and splitlines().
  • The Python split method is used to break a given string by the specified delimiter like a comma.
  • The split() 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 a comma as a separator.

See the last section of this tutorial for the 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 possible number.

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 in 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:

The code:

#A demo of string split method

strmet = "The string split method"
print (strmet.split())

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

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

An example of using a comma as a 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:

The code:

#A demo of string split method
strmet = "The string split, a Python method, for breaking strings"

print (strmet.split(','))

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 the above example, however, the maxsplit parameter is used as 1. See the code and output by clicking the link or image below:

The code for using the maximum parameter:

strmet = "The string split, a Python method, for breaking strings"

print (strmet.split(',',1))

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:

The code with for loop:

str_split = "The string split, a Python method, for breaking strings"

str_list = str_split.split(',')
for i in str_list:

print (i)

Output:

split 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 the 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:

The code for the split and rsplit:

str_tst = "The string split, a Python method, for breaking strings"
split_list = str_tst.split(',',1)
rsplit_list = str_tst.rsplit(',',1)

print ("List after split method: " ,split_list)
print ("List after rsplit method: " ,rsplit_list)

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 below:

Representation Description
\n Line Feed
\r Carriage Return
\r\n Carriage Return + Line Feed
\v or \x0b Line Tabulation
\f or \x0c Form Feed
\x1c File Separator
\x1d Group Separator
\x1e Record Separator
\x85 Next Line (C1 Control Code)
\u2028 Line Separator
\u2029 Paragraph Separator

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!”

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

str_tst = "Python is a powerful language. \n It has many string functions. \n Split is one of those!"
split_lines = str_tst.splitlines()

print (split_lines)

The output:

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

Atiq is the writer at jquery-az.com, an online tutorial website started in 2014. With a passion for coding and solutions, I navigate through various languages and frameworks. Follow along as we solve the mysteries of coding together!