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 String lstrip()
Python Program to Convert String to Datetime
Python Dictionary fromkeys()
Python Program to Convert Kilometers to Miles
Python Global Keyword
Python repr()
Generate a String
Python sorted()
Natural Language Processing with Python - Steven Bird & Ewan Klein & Edward Loper
Python Closures
Python *args and **kwargs
Python Object Oriented Programming
Intelligent Projects Using Python - Santanu Pattanayak
Python Program to Count the Number of Digits Present In a Number
Python Program to Compute all the Permutation of the String
Python sleep()
Python Program to Differentiate Between type() and isinstance()
Python Program to Find Factorial of Number Using Recursion
Python super()
Python Tuple count()
Check If a String Is Numeric in Java
Python if...else Statement
Python Input, Output and Import
Python Modules
Python Program to Check if a Key is Already Present in a Dictionary
Python Program to Represent enum
Deep Learning in Python - LazyProgrammer
Python Machine Learning Eqution Reference - Sebastian Raschka
Converting a Stack Trace to a String in Java
Python delattr()
Python String islower()
Python Program to Delete an Element From a Dictionary