Search

Python String Splitlines Method

post-title

Python splitlines() is an inbuilt method that returns a list when there is a line break in the string. It breaks the string at line boundaries and returns the split strings in the form of a list. There are different types of line breaks. For example \n(newline), \r(carriage return), \r\n(carriage return+new line) and many more.

Python String splitlines()

Python splitlines() function is used to split the lines at line boundaries. The splitlines() returns a list of lines in the string, including the line break(optional).

Syntax

string.splitlines([keepends])

Here the string is the primary string variable, which will be split using the splitlines() method.

keepends (optional): It is an optional parameter. When set to True, the line breaks are included in the resulting list. This can be a number, specifying a position of a line break or, can be any Unicode characters, like “\n”, “\r”, “\r\n”, etc. as boundaries for strings. The splitlines() method takes an optional parameter keepends, which take values as True and False. If the value is True, then the line break statements are also included in the returning list otherwise not.

Return value

It returns a list consisting of the elements of the main string with different elements breaking at line boundaries in the main string.

See the following code.

# app.py

h1 = 'Hello boy\n'
h1.splitlines(True)
print(h1)

Output

➜  pyt python3 app.py
Hello boy

➜  pyt

Example programs on splitlines() method in python

# app.py

h1 = "Hello I am\n a\n geek!"
h2 = "Python\nC++\nC\nJava\nKotlin"
h3 = "Virat Kohli \nis \nthe best"
h4 = "India is the best"
h5 = "I love chinese\r food!!"

print("Splitted list: ", h1.splitlines(), "\n")

print("Splitted list: ", h2.splitlines(), "\n")

print("Splitted list: ", h3.splitlines(), "\n")

print("Splitted list: ", h4.splitlines(), "\n")

print("Splitted list: ", h5.splitlines(), "\n")

Output

➜  pyt python3 app.py
Splitted list:  ['Hello I am', ' a', ' geek!']

Splitted list:  ['Python', 'C++', 'C', 'Java', 'Kotlin']

Splitted list:  ['Virat Kohli ', 'is ', 'the best']

Splitted list:  ['India is the best']

Splitted list:  ['I love chinese', ' food!!']

➜  pyt

Example 2: Write a program by passing parameters in the splitlines() method in python and show the output.

# app.py

h1 = "Hello I am\n a\n geek!"
h2 = "Python\nC++\nC\nJava\nKotlin"
h3 = "Virat Kohli \nis \nthe best"
h4 = "India is the best"
h5 = "I love chinese\r food!!"

print("Splitted list: ", h1.splitlines(True), "\n")

print("Splitted list: ", h2.splitlines(False), "\n")

print("Splitted list: ", h3.splitlines(True), "\n")

print("Splitted list: ", h4.splitlines(True), "\n")

print("Splitted list: ", h5.splitlines(True), "\n")

Output

➜  pyt python3 app.py
Splitted list:  ['Hello I am\n', ' a\n', ' geek!']

Splitted list:  ['Python', 'C++', 'C', 'Java', 'Kotlin']

Splitted list:  ['Virat Kohli \n', 'is \n', 'the best']

Splitted list:  ['India is the best']

Splitted list:  ['I love chinese\r', ' food!!']

➜  pyt

Example 3

# app.py

def str_len(string):

    li = string.splitlines()
    print(li)

    l = [len(element) for element in li]
    return l


string = "Hello\rthere\rHackTheStuff"
print(str_len(string))

Output

➜  pyt python3 app.py
['Hello', 'there', 'HackTheStuff']
[5, 5, 11]
➜  pyt

In the above code, we are learning how to use the concept of splitlines() to calculate the length of each word in a string.

Finally, Python String splitlines() Method Example is over.