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 Tuple index()
Python Program to Get the File Name From the File Path
Python setattr()
Python frozenset()
Python *args and **kwargs
Intelligent Projects Using Python - Santanu Pattanayak
Python String rsplit()
Python super()
Python Program to Find All File with .txt Extension Present Inside a Directory
Python Deep Learning Cookbook - Indra den Bakker
Python String count()
Python Program to Check If a String Is a Number (Float)
Convert String to int or Integer in Java
Deep Learning with Python - Francois Chollet
Python Program to Find the Largest Among Three Numbers
Python List
Python property()
Java – Reader to String
Python ord()
Python Program to Get the Last Element of the List
Python Program to Get a Substring of a String
String Set Queries
Python Program to Find Numbers Divisible by Another Number
Python vars()
Python Function Arguments
Python hex()
Python Set symmetric_difference_update()
Python sum()
Adding a Newline Character to a String in Java
Python Set isdisjoint()
Python List append()
Machine Learning Mastery with Python - Understand your data, create accurate models and work project...