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 sorted()
String Hashing
Most commonly used String methods in Java
Python Variables, Constants and Literals
Python Program to Find Numbers Divisible by Another Number
Python Program to Make a Flattened List from Nested List
Python Set difference()
Python String expandtabs()
Python sum()
Python List copy()
Python Machine Learning Cookbook - Practical solutions from preprocessing to Deep Learning - Chris A...
Python Program to Shuffle Deck of Cards
Introduction to Machine Learning with Python - Andreas C.Muller & Sarah Guido
Deep Learning in Python - LazyProgrammer
Python Program to Convert Kilometers to Miles
Python ascii()
Python List clear()
Python Program to Concatenate Two Lists
Python String ljust()
Python String isidentifier()
Python Program to Find All File with .txt Extension Present Inside a Directory
Python zip()
Python Program to Find LCM
Python String count()
Python Program to Check If a List is Empty
Python Program to Convert Bytes to a String
Python Dictionary update()
Python Program to Access Index of a List Using for Loop
Python min()
Python next()
Python 3 for Absolute Beginners - Tim Hall & J.P Stacey
Python open()