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 List
Python List clear()
Count Occurrences of a Char in a String
Python Program to Add Two Numbers
Python String rpartition()
Python Function Arguments
Python Program to Check If a List is Empty
Python String expandtabs()
Python Type Conversion and Type Casting
Python vars()
Python Program to Count the Number of Digits Present In a Number
Python Set remove()
Python Program to Measure the Elapsed Time in Python
Python reversed()
Convert char to String in Java
Python String replace()
Python all()
Python Package
Python String center()
Python Object Oriented Programming
Introduction to Scientific Programming with Python - Joakim Sundnes
Python break and continue
Python Set issuperset()
String Processing with Apache Commons Lang 3
Python Set symmetric_difference_update()
Python Get Current time
Python Program to Add Two Matrices
Python String isalnum()
Python Machine Learning Second Edition - Sebastian Raschka & Vahid Mirjalili
Python Sets
Python String lstrip()
Python super()