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 Package
Converting String to Stream of chars
Python Global, Local and Nonlocal variables
JavaScript Eval: run a code string
Machine Learning Applications Using Python - Cases studies form Healthcare, Retail, and Finance - Pu...
Python Set issuperset()
Python Program to Merge Two Dictionaries
Python property()
Python Program to Add Two Matrices
Python Program to Find the Factors of a Number
Python Program to Differentiate Between type() and isinstance()
Python Set clear()
Format ZonedDateTime to String
Python Program to Find Hash of File
Python oct()
Python String maketrans()
String Initialization in Java
Convert char to String in Java
Python memoryview()
Python Closures
Python String translate()
Python Program to Slice Lists
Python Program to Compute all the Permutation of the String
Python Set symmetric_difference()
Case-Insensitive String Matching in Java
Python reversed()
Python chr()
Python abs()
Python Program to Randomly Select an Element From the List
Java – String to Reader
Python List
Deep Learning with Python - A Hands-on Introduction - Nikhil Ketkar