Skip to content

[Python] How to use split() and splitlines() functions

In python, If we want to split a text with a specific character, and save the sub-paragraphs in the List data type, we can use split() function to do it simply.

The splitlines() function is very similar to split(), but splitlines() handles the newline symbol specifically, and can choose whether to keep the newline symbol during processing, which is very easy to use.


split() function

Suppose we have a following text.

text = \
"""
Today is a nice day.
This is a good day.
Hello! How are you?
"""


And we want to set the space symbol " " to be split point.

for word in text.split():
    print(word)



Output:

Today
is
a
nice
day.
This
is
a
good
day.
Hello!
How
are
you?


In addition to the default cutting with space symbol, we can also set the newline symbol \n to split.

for word in text.split('\n'):
    print(word)



Output:

Today is a nice day.
This is a good day.
Hello! How are you?

However, split() is not more convenient than splitlines() when cutting with a newline symbol \n. Let's take a look at an example of splitlines() below.


splitlines() function

The advantage of splitlines() over split() is that it is very likely that there are more than just simple line breaks like \n in our text, \r, \r\n, \n and so on will be treated as line breaks.

At the same time, splitlines() can also use the keepends parameter to determine whether to keep the newline symbol in the output result, which is very useful in some cases.

from pprint import pprint


for word in text.splitlines(keepends=True):
    pprint(word)


Output:

'\n'
'Today is a nice day.\n'
'This is a good day.\n'
'Hello! How are you?\n'

Here I used pprint() to display line breaks. It is worth mentioning that if keepends is True, the newline symbol is retained; otherwise, it is not retained.


References


Read More

Tags:

Leave a Reply