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 Prime Number
Java – Reader to String
Java InputStream to String
Python tuple()
Python Global, Local and Nonlocal variables
Why String is Immutable in Java?
String Initialization in Java
Python compile()
Python @property decorator
Python Set difference_update()
String Set Queries
Machine Learning with Python for everyone - Mark E.Fenner
Python List reverse()
Python map()
Python Dictionary clear()
Python String title()
Python if...else Statement
Jackson – Marshall String to JsonNode
Python Variables, Constants and Literals
Java Program to Permute All Letters of an Input String
Python Program to Check If Two Strings are Anagram
Python vars()
Python Program to Check if a Key is Already Present in a Dictionary
Convert char to String in Java
Python pass statement
Python Program to Get a Substring of a String
Python Program to Delete an Element From a Dictionary
Python classmethod()
Python Package
Python hasattr()
Python Program to Generate a Random Number
How to Get Started With Python?