Python String splitlines() method
Python String splitlines() method
The string.splitlines() method in Python splits the specified string and returns a list of the lines in the string, breaking at line boundaries.
Syntax
splitlines([keepends])
Parameter
keepends(optional): This parameter specifies if the line breaks should be included (True), or not (False). The Default value is not (False).
Return
This method returns a list of the lines in the string, breaking at line boundaries.
Example 1
# Python program explaining # the string. splitlines() method # initializing the string for \n and \r language = "Java\n C#\r Ruby\n Python\r C" # spliting the lines at line boundaries. print(language.splitlines()) print(language.splitlines(True)) # spliting the lines at line boundaries. language = "Java, C#, Ruby, Python, C" print(language.splitlines())
Output
['Java', ' C#', ' Ruby', ' Python', ' C'] ['Java\n', ' C#\r', ' Ruby\n', ' Python\r', ' C'] ['Java, C#, Ruby, Python, C']
Example 2
# Python program explaining # the string.splitlines() method # initializing the string for \n and \r fruits = "Apple\nRuby\nPython\nC" # no parameters has been passed # spliting the lines with whitespaces(\n) print(fruits.splitlines()) # including the line breaks(\n) print(fruits.splitlines(True))
Output
['Apple', 'Ruby', 'Python', 'C'] ['Apple\n', 'Ruby\n', 'Python\n', 'C']