Python - String splitlines() Method


Advertisements

The string 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.

Example:

In the below example, 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))

Output

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

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

String Methods
Advertisements