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 :
That means, if any of the above boundary value is the separator of the words of a string, we can use splitlines() method.
The syntax of the splitlines() method is as below :
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.
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) :
- For str1, both print methods with or without _keepends = True produced the same output. Because str1 _is only a single word.
- For str2, both print methods again printed the same output. _str2_ is consists of two words but both are separated by a blank space which is not a supported line boundary for splitlines.
- str3_ 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.
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 :
- We can define a separator type in split(). But we can’t define any separator type in splitlines().
- 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.
- 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 :
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.
- Python swap case of each character of a string
- Python program to pad zeros to a string
- Python tutorial to calculate the sum of two string numbers
- Python program to convert a string to an integer
- Python program to capitalize first letter of each words of a string
- Python program to find larger string among two strings