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 Armstrong Number
Python Dictionary update()
Python __import__()
Python callable()
Python bin()
Python Deep Learning Cookbook - Indra den Bakker
Python List remove()
Python max()
Python Program to Get a Substring of a String
Python Matrices and NumPy Arrays
Python object()
Python format()
Python Set union()
Python Operators
Python Program to Display the multiplication Table
Python type()
Python Operator Overloading
Python Program to Differentiate Between del, remove, and pop on a List
Convert String to int or Integer in Java
Python String zfill()
Python Program to Calculate the Area of a Triangle
Deep Learning with Applications Using Python - Navin Kumar Manaswi
Python Program to Print the Fibonacci sequence
Converting a List to String in Java
Python Custom Exceptions
Python Set intersection_update()
Jackson – Marshall String to JsonNode
Python Machine Learning Eqution Reference - Sebastian Raschka
Python Machine Learning - Sebastian Raschka
Python sum()
Python Set clear()
Python List pop()