Python String splitlines Method - Python Tutorial | Logic Practice
Python Course / Other / Python String splitlines Method

Python String splitlines Method

BLUF: This lesson on Python String splitlines Method provides a comprehensive guide to understanding and implementing this concept in Python. Whether you're a beginner or looking to refresh your knowledge, you'll find clear explanations and interactive code examples here.
Key Concept: Python String splitlines Method

Mastering Python String splitlines Method is essential for building efficient Python applications. Focus on the syntax and the best practices highlighted in this tutorial.

The Python method splitlines is utilized to divide a string according to its line breaks. It segments the string at the points where lines end and produces a list of the resulting substrings. The line separators may include new line characters (\n), carriage returns (\r), among others. Below is a table detailing the various line break characters that can be used to split the string.

This method splits on the given line boundaries.

Representation Description
n Line Feed
r Carriage Return
rn 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

Signature

Example

splitlines([keepends])

Parameters

keepends: This parameter is a boolean type that can take on the values of either True or False. It is not mandatory.

Return

It returns a comma separated list of lines.

Let us explore a few instances of the splitlines method to gain a clearer understanding of how it operates.

Python String splitlines Method Example 1

Example

# Python splitlines() method example
# Variable declaration
str = "Python is a programming language"
# Calling function
str2 = str.splitlines() # returns a list having single element
print(str)
print(str2)
str = "Java \n is a programming \r language"
str2 = str.splitlines() # returns a list having splitted elements
print(str2)

Output:

Output

Python is a programming language
['Python is a programming language']
['Python ', ' is a programming ', ' language']

Python String splitlines Method Example 2

By providing a value of True to the method, it results in the incorporation of line breaks within the string array. Refer to the example shown below.

Example

# Python splitlines() method example
# Variable declaration
str = "Java \n is a programming \r language"
# Calling function
str2 = str.splitlines(True) # returns a list having splitted elements
print(str2)

Output:

Output

['Java \n', ' is a programming \r', ' language']

Python String splitlines Method Example 3

Example

# Python splitlines() method example
# Variable declaration
str = "Java \n is a programming \r language for \r\n  software development"
# Calling function
str2 = str.splitlines() # returns a list having splitted elements
# Displaying result
print(str2)
# getting back list to string
print("".join(str2)) # now it does not contain any line breaker character

Output:

Output

['Python ', ' is a programming ', ' language for ', '  software development']
Java  is a programming  language for   software development

Input Required

This code uses input(). Please provide values below:

Logic Practice
Install Logic Practice
Add to home screen for a faster app-like experience