Python String splitlines() Method Tutorial

In this section, we will learn what String splitlines() method is and how to use it in Python.

What is String splitlines() Method in Python?

The Python string splitlines() method is used to split a string value into multiple sub-string values using newline character (\n) as the separator.

This means if the target string value has three lines, then calling this method on that string will return 3 sub-string values (each representing one line of the string) as a result.

Python String splitlines() Method Syntax:

string.splitlines(keepLineBreaks)

String splitlines() Method Parameter

The method takes one argument and that is a boolean value.

If the value is True, that means the method should keep the newline character as part of each sub-string. The value False means the method should drop the newline characters.

String splitlines() Method Return Value

The return value of this method is a reference to a list where the sub-string values of the target string are stored.

Example: using python string splitlines() method

s1 = "My name is John Doe \n I'm 1000 years old! "

print(s1.splitlines(True))

Output:

['My name is John Doe \n', " I'm 1000 years old! "]
Facebook
Twitter
Pinterest
LinkedIn

Top Technologies