Python 3 – String splitlines() Method
Python is an object-oriented, high-level programming language that has a wide range of applications. It is a popular language known for its simplicity, code readability, and excellent integration with other programming languages. Python’s libraries and frameworks are vast and efficient, making it an all-around programming language. One of the strings methods in Python 3 is the splitlines()
method.
Introduction
splitlines()
in Python is a method that separates a string into a list of lines. This method provides great help in working with text data, where a string contains multiple lines separated by newlines or line feeds. After splitting a string into lines, you can work on each line using loops, array functions, or further string manipulation.
Syntax
The splitlines()
method does not take any arguments.
string.splitlines([keepends])
Here,
– string
is the string that we want to split
– keepends
is an optional argument and defaults to False. It is a bool value that specifies whether the line breaks should be included with each line.
Example 1: Splitting a String with Multiple Lines
The following example demonstrates how to split a string with multiple lines separated by line feeds or newlines.
#creating a multi-line string
text = """hello
world
the sun is shining
"""
print(text.splitlines())
Output:
['hello ', 'world', 'the sun is shining']
The text
string contains three lines. The splitlines()
function splits the text into a list of lines. The resulting list does not contain the line separators.
Example 2: Splitting a String with Multiple Lines with Keepends Argument
In the next example, we use the optional argument keepends
to retain line separators in the output.
#creating a multi-line string
text = """hello
world
the sun is shining
"""
print(text.splitlines(keepends=True))
Output:
['hello \n', 'world\n', 'the sun is shining']
The keepends
parameter is a boolean value that specifies whether to keep line separators or not. By default, it is false, which means the line separators are excluded from the result. We pass the True
value to the keepends
parameter to keep the line separators in the output list.
Example 3: Splitting a String Without Line Breaks
In the next example, we check how the splitlines()
function behaves when there are no line breaks in the input string.
text = "this is a single line string"
print(text.splitlines())
Output:
['this is a single line string']
As we see from the output, this results in a single element list with the complete input string.
Example 4: Splitting a String with Different Line Breaks
In the last example, we check how the splitlines()
method handles strings containing multiple types of line breaks.
text = "dear\nmadam\r\ni hope\rthis finds you well"
print(text.splitlines())
Output:
['dear', 'madam', 'i hope', 'this finds you well']
The input string text
contains three different types of line breaks: \n
, \r
, and \r\n
. The splitlines()
function treats each of these differently and splits the string into four lines.
Conclusion
In conclusion, we learned about the splitlines()
method in Python 3. The splitlines()
method is useful when working with text data where the string contains multiple lines separated by newlines or line feeds. By default, it returns a list of strings with line separators removed. However, the optional keepends
argument can be used to keep the line separators with each line. We also saw that this method works differently with various line breaks. With this knowledge, we can work efficiently with text data in Python.