splitline() method is used to split the lines at line boundaries. The function returns a list of lines in the string, including the line break(optional).
Syntax :
string.splitlines([keepends])
Parameters :
keepends (optional): When set to True line breaks are included in the resulting list.
This can be a number, specifying the position of line break or, can be any Unicode characters, like “\n”, “\r”, “\r\n”, etc as boundaries for strings.
Return Value :
Returns a list of the lines in the string, breaking at line boundaries.
The below code shows the illustration of splitline() method.
Code #1
# Python code to illustrate splitlines() string = "Welcome everyone to\rthe world of Geeks\nGeeksforGeeks" # No parameters has been passed print (string.splitlines( )) # A specified number is passed print (string.splitlines(0)) # True has been passed print (string.splitlines(True)) |
Output :
['Welcome everyone to', 'the world of Geeks', 'GeeksforGeeks'] ['Welcome everyone to', 'the world of Geeks', 'GeeksforGeeks'] ['Welcome everyone to\r', 'the world of Geeks\n', 'GeeksforGeeks']
Code #2
# Python code to illustrate splitlines() string = "Cat\nBat\nSat\nMat\nXat\nEat" # No parameters has been passed print (string.splitlines( )) # splitlines() in one line print('India\nJapan\nUSA\nUK\nCanada\n'.splitlines()) |
Output :
['Cat', 'Bat', 'Sat', 'Mat', 'Xat', 'Eat'] ['India', 'Japan', 'USA', 'UK', 'Canada']
Practical Application:
In this code, we will understand how to use the concept of splitlines() to calculate the length of each word in a string.
# Python code to get length of each words def Cal_len(string): # Using splitlines() divide into a list li = string.splitlines() print (li) # Calculate length of each word l = [len(element) for element in li] return l # Driver Code string = "Welcome\rto\rGeeksforGeeks"print(Cal_len(string)) |
Output :
['Welcome', 'to', 'GeeksforGeeks'] [7, 2, 13]
Attention geek! Strengthen your foundations with the Python Programming Foundation Course and learn the basics.
To begin with, your interview preparations Enhance your Data Structures concepts with the Python DS Course.
Recommended Posts:
- String slicing in Python to check if a string can become empty by recursive deletion
- String slicing in Python to rotate a string
- Python | Check if a given string is binary string or not
- Python | Check if given string can be formed by concatenating string elements of list
- Python | Check if string ends with any string in given list
- Python | Sorting string using order defined by another string
- Python | Merge Tuple String List values to String
- Python | Sort each String in String list
- Python | Convert List of String List to String List
- Python - Length of shortest string in string list
- Python - Remove front K characters from each string in String List
- Python - Remove String from String List
- Python | Delimited String List to String Matrix
- String Alignment in Python f-string
- String to Int and Int to String in Python
- Pad or fill a string by a variable in Python using f-string
- Python - Filter String Tuples if String lengths equals K
- How to split a string in C/C++, Python and Java?
- String Template Class in Python
- Python String Methods | Set 1 (find, rfind, startwith, endwith, islower, isupper, lower, upper, swapcase & title)
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.

