Python String splitlines() Method – Tutorial with Examples

Python String splitlines() Method

The splitlines() method is a built-in method in Python that is used to split a string into a list of strings at line breaks. It is a useful method for working with text data that contains line breaks or for splitting a multi-line string into individual lines.

Syntax

The syntax of the splitlines() method is as follows:

string.splitlines([keepends])

Here, string is the string that we want to split, and keepends is an optional boolean parameter that specifies whether to include line breaks in the resulting list of strings. If keepends is True, line breaks are included; if it is False or not specified, they are not included.

Return Value

The splitlines() method returns a list of strings, where each element in the list is a line from the original string.

Examples

Here are three different examples of how to use the splitlines() method in Python:

Example 1: Splitting a String into Lines

The following example demonstrates how to use the splitlines() method to split a string into individual lines:

string = "This is a\nmulti-line\nstring."
lines = string.splitlines()
print(lines)

Output:

['This is a', 'multi-line', 'string.']

In this example, we define a multi-line string and use the splitlines() method to split it into individual lines. The resulting list of strings contains each line of the original string as a separate element.

Example 2: Keeping Line Breaks

The following example demonstrates how to use the splitlines() method to split a string into lines and include line breaks in the resulting list:

string = "This is a\nmulti-line\nstring."
lines = string.splitlines(keepends=True)
print(lines)

Output:

['This is a\n', 'multi-line\n', 'string.']

In this example, we use the optional keepends parameter to include line breaks in the resulting list of strings. The resulting list contains each line of the original string as a separate element, with the line breaks included.

Example 3: Splitting a String with Different Line Endings

The following example demonstrates how to use the splitlines() method to split a string into lines when the string uses different line endings:

string = "This is a\r\nmulti-line\rstring."
lines = string.splitlines()
print(lines)

Output:

['This is a', 'multi-line\rstring.']

In this example, we define a multi-line string that uses different line endings (a combination of carriage return and newline characters). The splitlines() method is still able to split the string into individual lines, but the resulting list of strings contains the line endings as part of the last line.

Use Cases

The splitlines() method is particularly useful for working with text data that contains line breaks or for splitting multi-line strings into individual lines. It can be used in a variety of scenarios, such as:

  • Reading and processing text files that contain line breaks.
  • Working with multi-line strings in Python scripts.
  • Manipulating text data in Python, such as cleaning and formatting data.

Overall, the splitlines() method is a versatile tool for working with text data in Python, and can be useful for a wide range of applications.

Leave a Reply

Your email address will not be published. Required fields are marked *