Python Tutorial Python Advanced Python References Python Libraries

Python String - splitlines() Method



The Python splitlines() method is used to split the string into a list using line break ("\n") as separator. It has one optional parameter which can be used to specify whether to keep or remove the line breaks in the result.

Syntax

string.splitlines(keeplinebreaks)

Parameters

keeplinebreaks Optional. specify whether to keep (True) or remove (False) the line breaks in the result. default value is False.

Return Value

Returns a list which contains elements as a result of splitting a given string using line break ("\n").

Example:

In the example below, splitlines() method is used to split the string by line break ("\n"). This method finally returns the result in a list.

MyString = "Python\nJava\nC++"
print(MyString.splitlines())

MyString = "Python\nJava\nC++"
print(MyString.splitlines(True))

The output of the above code will be:

['Python', 'Java', 'C++']
['Python\n', 'Java\n', 'C++']

❮ Python String Methods