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 :
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 :
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 few examples (The source code is available here) :
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 :
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 :
- 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