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 Add Two Numbers
Statistical Methods for Machine Learning - Disconver how to Transform data into Knowledge with Pytho...
Python Program to Randomly Select an Element From the List
Python Set pop()
Python Closures
Python String rjust()
Python bytes()
Python property()
Python Package
Python List reverse()
Python pow()
Python Program to Find HCF or GCD
Convert String to Byte Array and Reverse in Java
Check if a String is a Palindrome in Java
Python String title()
Python Program to Create a Countdown Timer
Python List
Python Operator Overloading
Python Keywords and Identifiers
Python List clear()
Learning scikit-learn Machine Learning in Python - Raul Garreta & Guillermo Moncecchi
Python Set intersection_update()
Python Program to Display the multiplication Table
Python Program to Generate a Random Number
Python Sets
String Hashing
Deep Learning in Python - LazyProgrammer
Convert char to String in Java
Python Program to Check Whether a String is Palindrome or Not
Python Program to Print Colored Text to the Terminal
Python Program to Get the Last Element of the List
Python List remove()