Generic selectors
Exact matches only
Search in title
Search in content
Search in posts
Search in pages
wb_sunny

How to Use the Python String splitlines() Method

The Splitlines() Method In Python

Introduction

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.splitlines([keepends])

Here,

  • 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

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

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.

Without keepends

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())

Output:

['Tim', 'Charlie', 'John', 'Alan']
['Welcome', '', 'to', 'AskPython\t!']
['Keyboard', 'Monitor', '', 'Mouse', 'CPU', 'Motherboard', 'Speakers', 'UPS']

Here,

  • 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.

With keepends

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))

Output:

['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.

Conclusion

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.

References