Python String splitlines() Method
Python splitlines() returns a list with each line as an element, separated by lines (‘\r’, ‘\r\n’, \n’), without line breaks if the argument keepsends is False, and with line breaks if True.
Python String splitlines() Syntax
Syntax of the splitlines() method.
str.splitlines([keepends])
Python String splitlines() Parameters
- keepends – if or not to remove line breaks (‘\r’, ‘\r\n’, \n’) in the output, defaults to False to not include line breaks, if True to keep line breaks.
Python String splitlines() Return Value
Returns a list containing each row as an element.
Python String splitlines() Example#1
The following example demonstrates the use of the splitlines() function.
string = "Welcome everyone to\r apidemos.com to study apis, good website."
# No parameters has been passed
print (string.splitlines( ))
# A specified number is passed
print (string.splitlines(0))
# True has been passed
print (string.splitlines(True))
Output:
Python String splitlines() Example#2
string = "Cat\nBat\nSat\nMat\nXat\nEat"
# No parameters has been passed
print (string.splitlines( ))
# splitlines() in one line
print('India\nJapan\nUSA\nUK\nCanada\n'.splitlines())
Output: