How do I split a multi-line string into multiple lines?

PythonServer Side ProgrammingProgramming

A string is a collection of characters that may be used to represent a single word or an entire phrase. Strings are useful in Python since they don't need to be declared explicitly and may be defined with or without a specifier. For dealing with and accessing strings, Python includes a variety of built-in methods and functions. A string is an object of the String class, which contains multiple methods because everything in Python is an object.

In this article, we are going to find out how to split a multi-line string into multiple lines in Python.

The first technique is to use the built−in splitlines method (). It accepts a multi-line string as input and outputs a split string that is divided at each new line. There are no parameters required. The splitlines() method is a built−in string method that is solely used to split new lines.

Example 1

In the program given below, we are taking a multi-line string as input and we are splitting the string at newline using the splitlines() method 

str1 = "Welcome\nto\nTutorialspoint"

print("The given string is")
print(str1)

print("The resultant string split at newline is")
print(str1.splitlines())

Output

The output of the above example is as follows −

The given string is
Welcome
to
Tutorialspoint
The resultant string split at newline is
['Welcome', 'to', 'Tutorialspoint']

Example 2

In the example given below, we are using the same splitlines() method for splitting at the newline but we are taking input in a different fashion 

str1 = """Welcome
To
Tutorialspoint"""

print("The given string is")
print(str1)

print("The resultant string split at newline is")
print(str1.splitlines())

Output

The output of the above example is given below −

The given string is
Welcome
to
Tutorialspoint
The resultant string split at newline is
['Welcome', 'to', 'Tutorialspoint']

Using split() method

The second technique is to use the built−in method split (). One argument is required: the character at which the provided text should be divided. If we wish to split on a new line, we should use the argument 'n'. Unlike the splitlines() function, the split() method can split at any character. We only need to send the character at which the string should be divided.

Example 1

In the example given below, we are taking a string as input and we are dividing the string at a new line using split() method 

str1 = "Welcome\nto\nTutorialspoint"

print("The given string is")
print(str1)

print("The resultant string split at newline is")
print(str1.split('\n'))

Output

The output of the above example is as shown below −

The given string is
Welcome
to
Tutorialspoint
The resultant string split at newline is
['Welcome', 'to', 'Tutorialspoint']

Example 2

In the example given below, we are using the same split() method for splitting at the newline but we are taking input in a different fashion 

str1 = """Welcome
To
Tutorialspoint"""

print("The given string is")
print(str1)

print("The resultant string split at newline is")
print(str1.split('\n'))

Output

The output of the above example is as shown below −

The given string is
Welcome
to
Tutorialspoint
The resultant string split at newline is
['Welcome', 'to', 'Tutorialspoint']
raja
Updated on 07-Dec-2022 10:29:59

Advertisements