Python splitlines() is an inbuilt method that returns a list when there is a line break in the string. It breaks the string at line boundaries and returns the split strings in the form of a list. There are different types of line breaks. For example \n(newline), \r(carriage return), \r\n(carriage return+new line) and many more.
Python splitlines() function is used to split the lines at line boundaries. The splitlines() returns a list of lines in the string, including the line break(optional).
string.splitlines([keepends])
Here the string is the primary string variable, which will be split using the splitlines() method.
keepends (optional): It is an optional parameter. When set to True, the line breaks are included in the resulting list. This can be a number, specifying a position of a line break or, can be any Unicode characters, like “\n”, “\r”, “\r\n”, etc. as boundaries for strings. The splitlines() method takes an optional parameter keepends, which take values as True and False. If the value is True, then the line break statements are also included in the returning list otherwise not.
It returns a list consisting of the elements of the main string with different elements breaking at line boundaries in the main string.
See the following code.
# app.py
h1 = 'Hello boy\n'
h1.splitlines(True)
print(h1)
➜ pyt python3 app.py
Hello boy
➜ pyt
# app.py
h1 = "Hello I am\n a\n geek!"
h2 = "Python\nC++\nC\nJava\nKotlin"
h3 = "Virat Kohli \nis \nthe best"
h4 = "India is the best"
h5 = "I love chinese\r food!!"
print("Splitted list: ", h1.splitlines(), "\n")
print("Splitted list: ", h2.splitlines(), "\n")
print("Splitted list: ", h3.splitlines(), "\n")
print("Splitted list: ", h4.splitlines(), "\n")
print("Splitted list: ", h5.splitlines(), "\n")
➜ pyt python3 app.py
Splitted list: ['Hello I am', ' a', ' geek!']
Splitted list: ['Python', 'C++', 'C', 'Java', 'Kotlin']
Splitted list: ['Virat Kohli ', 'is ', 'the best']
Splitted list: ['India is the best']
Splitted list: ['I love chinese', ' food!!']
➜ pyt
# app.py
h1 = "Hello I am\n a\n geek!"
h2 = "Python\nC++\nC\nJava\nKotlin"
h3 = "Virat Kohli \nis \nthe best"
h4 = "India is the best"
h5 = "I love chinese\r food!!"
print("Splitted list: ", h1.splitlines(True), "\n")
print("Splitted list: ", h2.splitlines(False), "\n")
print("Splitted list: ", h3.splitlines(True), "\n")
print("Splitted list: ", h4.splitlines(True), "\n")
print("Splitted list: ", h5.splitlines(True), "\n")
➜ pyt python3 app.py
Splitted list: ['Hello I am\n', ' a\n', ' geek!']
Splitted list: ['Python', 'C++', 'C', 'Java', 'Kotlin']
Splitted list: ['Virat Kohli \n', 'is \n', 'the best']
Splitted list: ['India is the best']
Splitted list: ['I love chinese\r', ' food!!']
➜ pyt
# app.py
def str_len(string):
li = string.splitlines()
print(li)
l = [len(element) for element in li]
return l
string = "Hello\rthere\rHackTheStuff"
print(str_len(string))
➜ pyt python3 app.py
['Hello', 'there', 'HackTheStuff']
[5, 5, 11]
➜ pyt
In the above code, we are learning how to use the concept of splitlines() to calculate the length of each word in a string.
Finally, Python String splitlines() Method Example is over.
Hi, My name is Harsukh Makwana. i have been work with many programming language like php, python, javascript, node, react, anguler, etc.. since last 5 year. if you have any issue or want me hire then contact me on [email protected]
How to send mail in laravel
Laravel has so many builtin features lik...How To Set Header In maatwebsite/excel Export In Laravel
Today, we are share with you how to set...Laravel 8 API authentication using Passport from the scratch
Laravel provides 2 ways API authenticati...How to merge two or more arrays into one array in PHP
Use the PHP array_merge() func...How to Create Animation Effects using pure CSS
Creating animation nowadays is becoming...