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:
Reading an HTTP Response Body as a String in Java
Python pow()
Python List Comprehension
Python Program to Convert Decimal to Binary Using Recursion
Python pass statement
How to get current date and time in Python?
Python isinstance()
CharSequence vs. String in Java
Java String Conversions
Python for Loop
Python for Programmers with introductory AI case studies - Paul Deitel & Harvey Deitel
Python bytes()
Python Program to Represent enum
Python Directory and Files Management
Python Program to Check Prime Number
Python String count()
Python Program to Find Sum of Natural Numbers Using Recursion
Most commonly used String methods in Java
Python frozenset()
Python String zfill()
Python compile()
Python sleep()
Case-Insensitive String Matching in Java
Python List clear()
Python memoryview()
Python Operators
Python exec()
Python String istitle()
Python String isdigit()
Python Program to Delete an Element From a Dictionary
Python Dictionary keys()
Python Sets