Table of Contents
The splitlines() method splits the string at line breaks and returns a list of lines in the string.
The syntax of splitlines() is:
str.splitlines([keepends])
1. splitlines() Parameters
splitlines() takes maximum of 1 parameter.
keepends (optional) – If keepends is provided and True, line breaks are also included in items of the list.
By default, the line breaks are not included.
2. Return Value from splitlines()
splitlines() returns a list of lines in the string.
If there are not line break characters, it returns a list with a single item (a single line).
splitlines() splits on the following line boundaries:
| Representation | Description |
|---|---|
| \n | Line Feed |
| \r | Carriage Return |
| \r\n | Carriage Return + Line Feed |
| \v or \x0b | Line Tabulation |
| \f or \x0c | Form Feed |
| \x1c | File Separator |
| \x1d | Group Separator |
| \x1e | Record Separator |
| \x85 | Next Line (C1 Control Code) |
| \u2028 | Line Separator |
| \u2029 | Paragraph Separator |
3. Example: How splitlines() works?
grocery = 'Milk\nChicken\r\nBread\rButter' print(grocery.splitlines()) print(grocery.splitlines(True)) grocery = 'Milk Chicken Bread Butter' print(grocery.splitlines())
Output
['Milk', 'Chicken', 'Bread', 'Butter'] ['Milk\n', 'Chicken\r\n', 'Bread\r', 'Butter'] ['Milk Chicken Bread Butter']
Related posts:
Python ascii()
Python Dictionary keys()
Python globals()
Python dir()
Deep Learning with Python - Francois Cholletf
Python String rjust()
Python String lstrip()
Python object()
Python Set discard()
How to get current date and time in Python?
Python Set difference_update()
CharSequence vs. String in Java
Python Dictionary popitem()
Python sorted()
Python frozenset()
Python Set intersection()
Deep Learning with Applications Using Python - Navin Kumar Manaswi
Python format()
Python Program to Find HCF or GCD
Python String endswith()
Python String center()
Python Program to Check Leap Year
Python Keywords and Identifiers
Python isinstance()
Python Set pop()
Python Shallow Copy and Deep Copy
Python Program to Find the Factors of a Number
Python Program to Print Hello world!
Python Operator Overloading
Python Program to Get File Creation and Modification Date
Python int()
Python Program to Iterate Through Two Lists in Parallel