Menu Close

Python String splitlines() Method

python string splitlines

In this tutorial, we are going to learn all about the Python string splitlines() method. Python string splitlines() method is used to split the string on line break and return the list.

In the previous tutorial, we have seen the Python string title() method to convert each character of each word in the upper case.

Python string splitlines() method

In Python, splitlines function is string built-in function which are used to split the string on line breaks and return the list.

Syntax

The syntax of Python string splitlines() method:-

string.splitlines(keeplinebreaks)

Parameter

splitlines function in Python accepts one parameter that is keeplinebreaks.

  • keeplinebreaks:- Optional. Specify True if you want to include linebreaks in the result otherwise specify False. By default it is False.

Python string splitlines example

Here we will take some examples to understand the python string splitlines method.

Example 1:

str = 'Programming \nfunda is a \neducational site.'
result = str.splitlines(True)
print(result)


Output:

['Programming \n', 'funda is a \n', 'educational site.']

Example 2:

str = 'Programming \nfunda is a \neducational site.'
result = str.splitlines(True)
print(result)

Output:

['Programming ', 'funda is a ', 'educational site.']

Example 3:

str = 'Programming \nfunda is a \neducational site.'
result = str.splitlines(True)
for i in result:
	print(i)

Output:

Programming 

funda is a 

educational site.

Example 4:

str = 'Programming \nfunda is a \neducational site.'
result = str.splitlines(False)
for i in result:
	print(i)

Output:

Programming 
funda is a 
educational site.

Conclusion

In this tutorial, you have learned all about the Python string splitlines method to split the string on line breaks along with examples.
I hope this article will help you. If you like this article, please share it with your friends who want to learn Python programming.

Other string methods


For More Information:- Click Here

Python String rstrip() Method
Python String translate() Method

Related Posts