Today in this tutorial, we are going to discuss the Python string splitlines() method.
Firstly, let us look at the basic definition of the method.
The Python String splitlines() Method
The Python string
splitlines() is a built-in method that returns a list of the lines in the string, breaking at line boundaries. Line breaks are not included in the resulting list unless
keepends is mentioned as true.
The syntax for using the
splitlines() method in Python is given below.
- str is the string object which we need to split into a list of lines,
- keepends when set
True, the line boundaries are included in the resultant list elements. Or else, the line breaks are not included.
The table for line boundary characters and their respective descriptions is given below.
Line Boundary Table
|Character||Representation In Python|
|\r\n||Carriage Return + Line Feed|
|\v or \x0b||Line Tabulation (Python 3.2 onwards)|
|\f or \x0c||Form Feed (Python 3.2 onwards)|
|\x85||Next Line (C1 Control Code)|
Working with the splitlines() Method in Python
Now that we have covered the basic definition and syntax for the
splitlines() method in Python, let us look at some examples. This will help us have a clear understanding of the topic.
As mentioned earlier, without mentioning the keepends parameter will result in the creation of list of splited lines excluding the line breaks or boundary characters.
Look at the example below.
#String initialisation string1 = "Tim\nCharlie\nJohn\nAlan" string2 = "Welcome\n\nto\r\nAskPython\t!" string3 = "Keyboard\u2028Monitor\u2029\x1cMouse\x0cCPU\x85Motherboard\x1eSpeakers\r\nUPS" #without keepends print(string1.splitlines()) print(string2.splitlines()) print(string3.splitlines())
['Tim', 'Charlie', 'John', 'Alan'] ['Welcome', '', 'to', 'AskPython\t!'] ['Keyboard', 'Monitor', '', 'Mouse', 'CPU', 'Motherboard', 'Speakers', 'UPS']
- We have declared three strings containing various words separated by different line breakers,
- We pass each one of them to the built-in
splitlines()method with keepends set to default (false). And print the resultant lists of splited lines.
As we can see from the output, as keepends was not set all the splited lines do not contain the line borders or boundary characters. For string2,
'\t' is included with the word
'Askpython\t' because it is not a boundary character( is not in the table).
Hence, the output is justified.
If we mention the keepends parameter as
True, the splited lines now would include the respective line breakers.
Let us modify our previous code(without keepends) by setting the keepends parameter as
True inside the Python string
splitlines() method. Look at the output carefully and try to notice the change from the previous one.
#String initialisation string1 = "Tim\nCharlie\nJohn\nAlan" string2 = "Welcome\n\nto\r\nAskPython\t!" string3 = "Keyboard\u2028Monitor\u2029\x1cMouse\x0cCPU\x85Motherboard\x1eSpeakers\r\nUPS" #with keepends print(string1.splitlines(keepends=True)) print(string2.splitlines(keepends=True)) print(string3.splitlines(keepends=True))
['Tim\n', 'Charlie\n', 'John\n', 'Alan'] ['Welcome\n', '\n', 'to\r\n', 'AskPython\t!'] ['Keyboard\u2028', 'Monitor\u2029', '\x1c', 'Mouse\x0c', 'CPU\x85', 'Motherboard\x1e', 'Speakers\r\n', 'UPS']
As expected, for the same strings the
splitlines() output includes all the boundary characters.
Thus in this tutorial, we got to know about the built-in Python string
splitlines() method, what it does and how it works.
For any questions regarding this topic, feel free to post them in the comments below.
- Python Documentation on string splitlines(),
- Python String splitlines() – Journal Dev Post,
- How to split a python string on new line characters – Stack Overflow Question.