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:
Deep Learning from Scratch - Building with Python form First Principles - Seth Weidman
Python String ljust()
Intelligent Projects Using Python - Santanu Pattanayak
Python Program to Iterate Through Two Lists in Parallel
Python Program to Iterate Over Dictionaries Using for Loop
Python staticmethod()
Python Program to Copy a File
Python List Comprehension
Python eval()
Python Program to Find Sum of Natural Numbers Using Recursion
Python Dictionary pop()
Python property()
Python Program to Print Output Without a Newline
Python Program to Append to a File
APIs in Node.js vs Python - A Comparison
Python String upper()
Python next()
Python enumerate()
Python String isidentifier()
Python exec()
Map to String Conversion in Java
Python Data Analytics with Pandas, NumPy and Matplotlib - Fabio Nelli
Python Program to Find ASCII Value of Character
Python Program to Remove Duplicate Element From a List
Python Program to Check if a Number is Positive, Negative or 0
Python Program to Convert Two Lists Into a Dictionary
Python Deep Learning Cookbook - Indra den Bakker
Python Program to Get File Creation and Modification Date
Python Dictionary
String Hashing
Split a String in Java
Python Program to Find the Factorial of a Number