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 Program to Print Hello world!
Python Program to Get the Last Element of the List
Python Program to Slice Lists
Python chr()
Python List append()
Python List insert()
Python del Statement
Python String translate()
Python Program to Iterate Through Two Lists in Parallel
Python Program to Print all Prime Numbers in an Interval
Python Errors and Built-in Exceptions
Python callable()
Python hex()
Java Program to Permute All Letters of an Input String
Deep Learning with Python - Francois Cholletf
Python String islower()
Python Set issubset()
Python Program to Make a Flattened List from Nested List
APIs in Node.js vs Python - A Comparison
Python Multiple Inheritance
Python String rfind()
Python List copy()
Python Program to Print Colored Text to the Terminal
Java – Reader to String
Python int()
Python String isdecimal()
Python iter()
Python String encode()
Python List extend()
String Processing with Apache Commons Lang 3
Python map()
Python List Comprehension