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
Python bytearray()
Python Exception Handling Using try, except and finally statement
Java String Conversions
Python String count()
Python String isdigit()
Python Program to Check If Two Strings are Anagram
Python Matrices and NumPy Arrays
Python hex()
Array to String Conversions
Python String capitalize()
Python Closures
Python oct()
Python String isprintable()
Python Deep Learning - Valentino Zocca & Gianmario Spacagna & Daniel Slater & Peter Roelants
Python globals()
Python Data Structures and Algorithms - Benjamin Baka
Python Program to Print Output Without a Newline
Python Program to Represent enum
Python Machine Learning Second Edition - Sebastian Raschka & Vahid Mirjalili
Python Artificial Intelligence Project for Beginners - Joshua Eckroth
Python Set difference_update()
Python classmethod()
Python Program to Check if a Number is Odd or Even
Python Program to Sort a Dictionary by Value
Python Program to Convert Kilometers to Miles
Python String rindex()
Python Namespace and Scope
Python Recursion
Python Errors and Built-in Exceptions
Map to String Conversion in Java
Python Dictionary items()