Python example program to split a string at linebreak using splitlines

Python split a string at linebreak using splitlines() :

Line split or string split is one of the most common problems we faced in our development journey. For example, the server is sending us a list of comma separated values and we need to split all the values and put them in a list. The easiest way to solve this problem is to split the string.

However, in this tutorial, we are going to discuss a different python method called splitlines().

splitlines() method is used to split a string at line breaks. It returns the list of all lines in the given string.

This method takes one parameter. If the parameter is True, line breaks will be included in the result list. Else, line breaks will not be included.

Following are the line boundaries that this method splits :

python splitlines

That means, if any of the above boundary value is the separator of the words of a string, we can use splitlines() method.

Syntax of splitlines() :

The syntax of the splitlines() method is as below :

str.splitlines([keepends])

You can see that this is a built-in method and we don’t need to import any module to use it. The method splitlines() takes one optional parameter keepends. As mentioned above, if we pass True as this parameter, the line breaks will be included in the returned list.

The default value for keep ends is_ ‘False’_, i.e. if we don’t pass anything, it will not include the line break in the returned list.

Example of splitlines :

Let’s try to understand how splitlines() method works with a few examples

str_1 = "HelloWorld"

str_2 = "Hello World"

str_3 = "one\ntwo\rthree\r\nfour\vfive\x0bsix\fseven\x0ceight\x1cnine\x1dten\x1eeleven\x85twelve\u2028thirteen\u2029"

print("str_1 : ")
print(str_1.splitlines())
print(str_1.splitlines(keepends = True))

print("str_2 : ")
print(str_2.splitlines())
print(str_2.splitlines(keepends = True))

print("str_3 : ")
print(str_3.splitlines())
print(str_3.splitlines(keepends = True))

(The source code is also available here) :

python splitlines

python splitlines example

Explanation :

  1. For str_1, both print methods with or without keepends = True produced the same output. Because _str_1 _is only a single word.

  2. For str_2, both print methods again printed the same output. str_2 is consists of two words but both are separated by a blank space which is not a supported line boundary for splitlines.

  3. str_3 contains all supported line boundaries that we have shown above. As you can see that the first print statement separated each word from the string and the second print statement separated the words but added the line boundaries with each one.

Difference between splitlines and split :

split method is more useful if you want to split the strings using a specific separator character. Both splitlines and split methods are different. Following are the main differences between them :

  1. We can define a separator type in split(). But we can’t define any separator type in splitlines().

  2. We can provide a value of ‘maxsplit’ to the split() method. This variable is to indicate how many splits need to be done. But for splitlines(), we don’t have any such parameter available.

  3. If we are passing a delimiter string to split(), and if the string is an empty string, it will return one list with an empty string. For example :

python splitlines

Conclusion :

splitlines() method is useful if you want to split a string at its line breaks. One more good option is that you can remove or add the line breaks with the final words.

The above examples are written for Python-3. Try to run them and if you have any queries, drop one comment below.

You might also like :