Python String splitlines()
The Python splitlines()
method is used to break the given string at line boundaries, for example the \n
(newline characters), or \r
(carriage return), etc.
-
This is an inbuilt method of string in Python.
-
When we break the string, then different lines are compiled into a list and that is returned by this function. So we can say it returns a list of splitted lines.
-
Some fo the different types of line breaks are \n
(newline character), \r
(carriage return), \r\n
(carriage return+new line). We have a complete table specifying all the characters which specify the line boundaries.
Python String splitlines()
: Syntax
Below we have a basic syntax of String splitlines()
method in Python:
string.splitlines([keepends])
Note: In the above syntax, string is used to denote the primary string. Also, we do not have to specify which line boundary character should this function look for, as it autoamtically looks for all of them.
Python String splitlines()
: Parameters
-
keepends
This is an optional parameter, which can take either of the two values: true or false.
When This parameter is set to True, the line breaks are included in the resulting list. It can be a number, specifying a position of a line break or, it can be any unicode characters, like \n
, \r
, \r\n
, etc. which acts as boundaries for strings.
Python String splitlines()
: Returned Values
This method is returns a list that consists of the lines of the main string which are broken down based on different line boundaries characters present in the main string.
Python String splitlines()
: Basic Example
Below we have an example to show the working of String splitlines()
method:
str1 = 'Hello boy\nHow are you?'
print(str1.splitlines(True))
The Output for the above will be:
['Hello boy', 'How are you?']
Python String splitlines()
: Another Example
Lets have another example, with more variety of strings:
str1 = "India is a diverse country.\r\nWith many different religions and cultures"
str2 = "I love chinese\r food!!"
print("Splitted list: ", str1.splitlines())
print("Splitted list: ", str2.splitlines())
The Output for the same is given below:
Splitted list: ['India is a diverse country.', 'With many different religions and cultures']
Splitted list: ['I love chinese', ' food!!']
Supported Line Boundary characters for splitlines()
method:
The splitlines()
method split lines on the following line boundary characters:
Representation |
Description |
\n |
indicates newline character |
\r |
indicates Carriage Return |
\r\n |
indicates Carriage Return + new line |
\v or \x0b |
indicates Line Tabulation |
\f or \x0c |
indicates Form Feed |
\x1c |
indicates File Separator |
\x1d |
indicates Group Separator |
\x1e |
indicates Record Separator |
\x85 |
indicates Next Line (C1 Control Code) |
\u2028 |
indicates Line Separator |
Calculate character length of each word in a String:
In the code given below, we are using the concept of splitlines()
to calculate the length of each word in a string:
def str_len(string):
li = string.splitlines()
print(li)
l = [len(element) for element in li]
return l
string = "Hello\rthere\rStudytonight!!!!"
print(str_len(string))
The Output for the same will be:
['Hello', 'there', 'Studytonight!!!!']
[5, 5, 16]
Time for a Live Example!
Now it's time to understand the splitlines()
method with the help of a Live Example!
Summary
In this tutorial, we learned splitlines()
method of strings in Python that is used to return a list of lines after breaking the given string into lines at line boundaries.