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 Find the Factorial of a Number
Python Program to Find All File with .txt Extension Present Inside a Directory
Python Set difference()
Python ord()
Statistical Methods for Machine Learning - Disconver how to Transform data into Knowledge with Pytho...
Converting String to Stream of chars
Python Program to Catch Multiple Exceptions in One Line
Python Program to Transpose a Matrix
Python Program to Merge Two Dictionaries
Python Program to Find the Factors of a Number
Python String isidentifier()
Python all()
Python Dictionary keys()
Python for Loop
Python complex()
Python hash()
Python String isnumeric()
Python Program to Check Leap Year
Python String istitle()
Python Program to Remove Punctuations From a String
Python delattr()
Python Package
Python Program to Create a Long Multiline String
Python Program to Add Two Matrices
Python id()
Python String expandtabs()
Python Program to Shuffle Deck of Cards
Python Set difference_update()
Python Program to Find the Size (Resolution) of a Image
Python Dictionary popitem()
Python bool()
Converting String to Stream of chars