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 len()
Python String maketrans()
Python Program to Find Sum of Natural Numbers Using Recursion
Python Program to Find ASCII Value of Character
Generate a String
Python String index()
Deep Learning with Applications Using Python - Navin Kumar Manaswi
Python bin()
Applied Text Analysis with Python - Benjamin Benfort & Rebecca Bibro & Tony Ojeda
Python Anonymous / Lambda Function
Python hash()
Python String isdecimal()
Python Program to Get the Class Name of an Instance
Python Program to Convert Celsius To Fahrenheit
Python Program to Find All File with .txt Extension Present Inside a Directory
Python Set difference()
Python dict()
Python String title()
Python Program to Find HCF or GCD
Python File I/O Operation
Python float()
Python List index()
Python Program to Randomly Select an Element From the List
Machine Learning Applications Using Python - Cases studies form Healthcare, Retail, and Finance - Pu...
Python if...else Statement
Python Program to Count the Number of Each Vowel
Python staticmethod()
Python list()
Encode a String to UTF-8 in Java
Python print()
Python Program to Check Whether a String is Palindrome or Not