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 Check Whether a String is Palindrome or Not
Python filter()
Python range()
Java String Conversions
Python String index()
String Set Queries
Python break and continue
Python Program to Differentiate Between del, remove, and pop on a List
Python Program to Find All File with .txt Extension Present Inside a Directory
Python Program to Shuffle Deck of Cards
Python round()
Python Program to Copy a File
Python Package
Python Program to Find HCF or GCD
Python String isnumeric()
Python Program to Count the Number of Digits Present In a Number
Python Operator Overloading
Deep Learning with Python - Francois Chollet
Python len()
Python pass statement
Python String format()
Python Program to Find LCM
Python Set issuperset()
Python abs()
Python Program to Slice Lists
Python String find()
Python String lstrip()
Python @property decorator
Python String isdigit()
Python Program to Make a Flattened List from Nested List
Python Strings
Python Set intersection_update()