Python string splitlines() method
Python String splitlines() Method
Description
The splitlines() method splits a string at its line breaks. It returns a list containing the lines in the string. By default, line breaks are not included.
Line breaks can be tokens such as n
, r
, rn
, and f
.
Syntax
var.splitlines(keepends)
Parameters
- keepends − If set to True, line break tokens are included; otherwise, they are not. The default value is False.
Return Value
This method returns a list consisting of the lines in the string.
Example
var = '''Explicit
is
better than
implicit'''
var1 = var.splitlines()
print ("Original string:", var)
print ("Line list:", var1)
var = "HellonPython"
var2 = var.splitlines(True)
print ("Original string:", var)
print ("Line list:", var2)
var = '''Explicit
is
better than
implicit'''
var3 = var.splitlines(True)
print ("Original string:", var)
print ("Line list:", var3)
When you run this program, it will produce the following output −
Original string: Explicit
is
better than
implicit
Line list: ['Explicit', 'is', 'better than', 'implicit']
Original string: Hello
Python
Line list: ['Hellon', 'Python']
Original string: Explicit
is
better than
implicit
Line list: ['Explicitn', 'isn', 'better thann', 'implicit']