Python String splitlines()

The splitlines() method splits the string at line breaks and returns a list of lines in the string.

The syntax of splitlines() is:


1. splitlines() Parameters

splitlines() takes maximum of 1 parameter.

keepends (optional) – If keepends is provided and True, line breaks are also included in items of the list.

By default, the line breaks are not included.

2. Return Value from splitlines()

splitlines() returns a list of lines in the string.

If there are not line break characters, it returns a list with a single item (a single line).

splitlines() splits on the following line boundaries:

\nLine Feed
\rCarriage Return
\r\nCarriage Return + Line Feed
\v or \x0bLine Tabulation
\f or \x0cForm Feed
\x1cFile Separator
\x1dGroup Separator
\x1eRecord Separator
\x85Next Line (C1 Control Code)
\u2028Line Separator
\u2029Paragraph Separator

3. Example: How splitlines() works?

grocery = 'Milk\nChicken\r\nBread\rButter'


grocery = 'Milk Chicken Bread Butter'


['Milk', 'Chicken', 'Bread', 'Butter']
['Milk\n', 'Chicken\r\n', 'Bread\r', 'Butter']
['Milk Chicken Bread Butter']