The splitlines() method splits the string at line breaks and returns a list.
Example
# \n is a line boundary
sentence = 'I\nlove\nPython\nProgramming.'
# returns a list after spliting string at line breaks
resulting_list = sentence.splitlines()
print(resulting_list)
# Output: ['I', 'love', 'Python', 'Programming.']
splitlines() Syntax
The syntax of the splitlines() method is:
string.splitlines([keepends])
Here, keepends can be True or any number.
splitlines() Parameters
The splitlines() method can take a single parameter:
- keepends(optional) - it determines whether line breaks are included in the resulting list or not. It's value can be
Trueor any number.
splitlines() Return Value
The splitlines() method 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).
Note: The splitlines() method splits on the following line boundaries:
| Representation | Description |
|---|---|
| \n | Line Feed |
| \r | Carriage Return |
| \r\n | Carriage Return + Line Feed |
| \v or \x0b | Line Tabulation |
| \f or \x0c | Form Feed |
| \x1c | File Separator |
| \x1d | Group Separator |
| \x1e | Record Separator |
| \x85 | Next Line (C1 Control Code) |
| \u2028 | Line Separator |
| \u2029 | Paragraph Separator |
Example 1: Python String splitlines()
# '\n' is a line break
grocery = 'Milk\nChicken\nBread\rButter'
# returns a list after splitting the grocery string
print(grocery.splitlines())
Output
['Milk', 'Chicken', 'Bread', 'Butter']
In the above example, we have used the splitlines() method to split the grocery string i.e. 'Milk\nChicken\r\nBread\rButter' at the line breaks.
Here, grocery.splitlines() splits grocery at line break '\n' and returns a list '['Milk', 'Chicken', 'Bread', 'Butter']' after removing the line break.
Example 2: splitlines() with Multi Line String
We can also split the lines from multi line strings using the splitlines() method. For example,
# multi line string
grocery = '''Milk
Chicken
Bread
Butter'''
# returns a list after splitting the grocery string
print(grocery.splitlines())
Output
['Milk', 'Chicken', 'Bread', 'Butter']
Here, the splitlines() method splits the multi line string grocery and returns the list ['Milk', 'Chicken', 'Bread', 'Butter'].
Example 3: Passing Boolean Value in splitlines()
grocery = 'Milk\nChicken\nBread\rButter'
# returns a list including line breaks
resulting_list1 = grocery.splitlines(True)
print(resulting_list1)
# returns a list without including line breaks
resulting_list2 = grocery.splitlines(False)
print(resulting_list2)
Output
['Milk\n', 'Chicken\n', 'Bread\r', 'Butter'] ['Milk', 'Chicken', 'Bread', 'Butter']
In the above example, we have passed Boolean values True and False in the splitlines method to split 'Milk\nChicken\nBread\rButter'.
Here, in the method on passing:
True- returns a list including linebreaks in all items i.e.'['Milk\n', 'Chicken\n', 'Bread\r', 'Butter']'False- returns a list without including linebreaks in the items i.e.['Milk', 'Chicken', 'Bread', 'Butter']
Example 4: Passing Number in splitlines()
The splitlines() method takes an integer value as parameter. Here, 0 represents True and other positive or negative numbers indicate False. For example,
grocery = 'Milk\nChicken\nBread\rButter'
# returns list including line breaks
resulting_list1 = grocery.splitlines(0)
print(resulting_list1)
# returns list without including line breaks
resulting_list2 = grocery.splitlines(5)
print(resulting_list2)
Output
['Milk\n', 'Chicken\n', 'Bread\r', 'Butter'] ['Milk', 'Chicken', 'Bread', 'Butter']
Also Read: