Python String splitlines: The Complete Guide

Python splitlines() function 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 splitlines

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

Syntax

string.splitlines([keepends])

Arguments

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

keepends (optional): It is an optional parameter. The line breaks are included in the resulting list when set to True. This can be a number specifying a position of a line break or can be any Unicode characters, like “\n”, “\r”, “\r\n”, etc.,

The splitlines() method takes an optional parameter keeping, which takes values as True and False. If the value is True, the line break statements are also included in the returning list.

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

The splitlines() 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 3

# app.py

def str_len(string):

    li = string.splitlines()
    print(li)

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


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

Output

➜  pyt python3 app.py
['Hello', 'there', 'AppDividend']
[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.

That’s it for this tutorial.

Leave a Comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.