Split Multi-Line String into multiple Lines in Python

In this Python tutorial we are going to learn about methods through which we can split a multi-line string into multiple lines in Python.

Table Of Contents

But first what is a multi-line string ?

A multi-line string is a string enclosed between triple single quote or triple double quotes, everything inside it is considered as a mulit-line string.

EXAMPLE :

Advertisements
multiline_str = '''This is an example of multi line string.
You are learning about multi-line strings on a python tutorial page of thisponter.com.'''

Now we will look about some methods through which we can split a multi-line string into multiple lines and store them in some other variable or list.

Method 1 : Using splitlines()

First method that we can use to split a multi-line string into multiple lines using Python Programming language is the str.splitlines() function. This function is used to split the multi-lines string at line boundries and returns a list of lines in the string. It gets an optional parameter which is keepends, provide it as True when line breaks with Unicode Characters like “\n” is required in the resulting list of strings.

Lets see an Example :

CODE :

# Initializing a multiline string by enclosing
# string in three single quotes.
multiline_str = '''This is an example of multi line string.
You are learning about multi-line strings.
Checkout other tutorials on thisponter.com.'''

# The type() will print the type of var 
# multiline_str which happens to of class string
print(type(multiline_str))

# Storing multi-lines in splited_str using splitlines()
splited_str = multiline_str.splitlines(True)

# type() will return the type of var splited_str
# which happens to be of class list
print(type(splited_str))

# printing the splited string 
print(splited_str)

OUTPUT :

<class 'str'>
<class 'list'>
['This is an example of multi line string.\n',
 'You are learning about multi-line strings.\n',
 'Checkout other tutorials on thisponter.com.']

In above code and output, we have successfully splited a multi-line string into multiple lines using splitlines() method of string class. By using this method we stored the given multi-line string into mutli-lines in a list named splited_str, you can confirm this by looking at the class of var splited_str which is of class list.

Method 2 : Using replace() method

Next method that we can use to split a multi-line string into multiple lines is the replace() method of class string. It recieves three parameters :

  • Old : Old value which you want to replace
  • new : new value which you want to replace
  • count : Optional , how many occurrence of old value you want to replace. Default value is all occurrence.

See the example code below :

CODE :

# initializing a multiline string by 
# enclosing string in three single quotes.
multiline_str = '''This is an example of multiline string.
SECOND LINE IN CAPTIAL LETTERS.
third line in small letters'''

# type() will print the type of var multiline_str
# which happens to of class string
print(type(multiline_str))

# replacing old value('.') with new value ('\n') which is new line.
splited_str = multiline_str.replace('.', '\n')

print(splited_str)

OUTPUT :

<class 'str'>
This is an example of multiline string

SECOND LINE IN CAPTIAL LETTERS

third line in small letters

In above code and output you can see we have successfully splited a multi-line string into multiple lines using replace() method of class string. We replaced all the full stop(‘.’) with \n which stands for the new line character used to create new lines.It indicates that the line ends here and the remaining characters would be displayed in a new line.

Summary

In this tutorial we learned about two different methods through which we can split a mutli line string into multiple lines in Python Programming language. In first method we used splitline() method to split a multi-line string and store it into a list. In second method we used replace() method to create new lines with ‘\n’(the new line character) and replacing fullstops where a sentance ends with a new line character which creates a new line. You can always use any method according to your need,but the easisest way to do is to use method 1 (splitline()) which will return every line as an element of a list.

Also i have used Python 3.10.1 for writing examples. Type python –version to check your python version. Always try to write & understand example codes. Happy Coding.

Advertisements

Thanks for reading.

Leave a Comment

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Scroll to Top