Python multi line strings [5 Methods Explained]

 

Introduction to Python multi line strings

In Python, Strings are arrays of bytes representing Unicode characters. However, Python does not have a character data type, a single character is simply a string with a length of 1. Square brackets can be used to access elements of the string. In this tutorial, we will learn about Python multi line strings. We will cover various ways to write multi line strings in Python language. Most probably we will discuss five different ways along with solving examples. In a nutshell, this tutorial will contain all the necessary and common methods that most of the programmers used to create multi line strings.

 

Getting Started with Python multi line strings

Python multi line strings mean strings that are in more than one line. It is easy to generate strings in a single line but there are special methods to create multi-line strings. The following is an example of a multi-line string in Python.

Advertisement
welcome 
to 
golinuxcloud

We will cover five different methods including, three double quotes, three single quotes, brackets, backslash, and join() method to create multi line strings.

 

Method-1: Python multi line strings by using three single quotes

Single quotation marks are used to define a string in Python. We can use three single quotes to define multi line strings. For example, the following will be the simple syntax of python multi-line strings using three single quotes.

'''This is
Multi-line  string
In Python'''

Notice that in the above example a multi-line sentence is wrapped inside three single quotes.

 

Example of multi line strings by using three single quotes

Now let us take an example and see how we can use three single quotes to write multi-line strings in Python. See the example below:

# python multi line strings
string1 = '''This is 
going to be 
multi line string'''
string2 = '''############
Welcone to 
Go linux Cloud!!'''
# printing the strings
print(string1)
print(string2)

Output:

This is
going to be
multi line string
############
Welcone to
Go linux Cloud!!

In the example above, we have created multi-line strings by using three single quotes and then stored them in variables and finally print those variables.

Advertisement

 

Method-2: Python multi line strings using three double quotes

A double string is also used to define a string in Python. Similar to the three single quotes, three double quotes are also used to define multi-line strings in Python. The simple syntax looks like this:

"""This is
multi-line
String in Python"""

Notice that the strings are wrapped inside three double quotes which makes it a multi-line string.

 

Example of Python multi line strings using three double quotes

Now let us take an example and see how we can use three double quotes in Python to create multi line strings. See the example below:

# python multi line strings using three double quotes
string1 = """This is 
going to be 
multi line string"""
string2 = """############
Welcone to 
Go linux Cloud!!"""
# printing the strings
print(string1)
print(string2)

Output:

This is
going to be
multi line string
############
Welcone to
Go linux Cloud!!

In the above example, we have first created multi-line strings using three double quotes and then stored them in variables, and finally, print those variables.

 

Method-3: Python multi line strings using brackets

The brackets can be used to create multiline strings and split the strings together in Python. The only drawback of this technique is that the user needs to explicitly mention the spaces between the multiline strings. The simple syntax of the python multi line strings using brackets looks like this:

("This is "
 "multi line " 
"string in Python")

Each of the strings inside the brackets will be treated as single and separate line strings.

 

Example of multi line strings using brackets

Now let us take examples and see how we can use the Python brackets to create multi-line strings. See the example below:

Advertisement
# python multi line strings using= brackets
string1 = ("This is "
 "going to be " 
 "multi line string")
string2 = ("############" 
" Welcone to " 
" Go linux Cloud!!")
# printing the strings
print(string1)
print(string2)

Output:

This is going to be multi line string
############ Welcone to Go linux Cloud!!

Notice that in the program above, we successfully created a multi-line string using the brackets and double-quotes. We can also use single quotes instead of double. See the example below:

# python multi line strings using= brackets
string1 = ('This is '
 'going to be ' 
 'multi line string')
string2 = ('############'
' Welcone to ' 
' Go linux Cloud!!')
# printing the strings
print(string1)
print(string2)

Output:

This is going to be multi line string
############ Welcone to Go linux Cloud!!

Notice that this time we had used single quotes instead of double. If we want to print the strings in multi-line along with creating multi-line strings, then we have to use the line break after each line. See the example below:

string1 = ('This is \n'
 'going to be\n ' 
 'multi line string')
string2 = ('############\n'
' Welcone to \n' 
' Go linux Cloud!!')
# printing the strings
print(string1)
print(string2)

Output:

This is
going to be
multi line string
############
Welcone to
Go linux Cloud!!

In the above program, we have used the line break to print it as it is shown in the program.

 

Method-4: Python multi line strings using backslash

In Python strings, the backslash "\" is a special character, also called the "escape" character. It is used in representing certain whitespace characters: "\t" is a tab, "\n" is a new line, etc. We can use the backslash to define multi-line strings. The syntax of defining python multi line strings using backslash looks like this:

"This is "\
"multi line "\
"string in Python"\

Notice that we have used the backslash after each string to make it a multi-line string.

Advertisement

 

Example of Python multi line strings using backslash

Now let us take an example and see how we can use the backslash to create a multi-line string in Python. See the example below:

# python multi line strings using backslash
string1 ='This is'\
 ' going to be ' \
 'multi line string'
string2 = '############'\
' Welcone to ' \
' Go linux Cloud!!'
# printing the strings
print(string1)
print(string2)

Output:

This is going to be multi line string
############ Welcone to Go linux Cloud!!

Notice that we have used the backslash after each string to make it a multi-line string. We can also use double quotes instead of the single one and will get the same result. See the example below:

# python multi line strings using backslash
string1 ="This is"\
 "going to be " \
 "multi line string"
string2 = "############"\
" Welcone to "\
" Go linux Cloud!!"
# printing the strings
print(string1)
print(string2)

Output:

This is going to be multi line string
############ Welcone to Go linux Cloud!!

It python both the single and double quotation marks are used to define strings, so we can use either of them. If we want to print our multi-line string as it is, then we have to add the line break after each sentence. See the example below:

string1 ="This is\n"\
 "going to be \n" \
 "multi line string"
string2 = "############\n"\
" Welcone to \n"\
" Go linux Cloud!!"
# printing the strings
print(string1)
print(string2)

Output:

This is
going to be
multi line string
############
Welcone to
Go linux Cloud!!

Notice that we were able to print the multiline strings as they are by using the escape line strategy.

 

Method-5: Python multi line strings using join() method

Python String join() method is a string method and returns a string in which the elements of the sequence have been joined by the str separator. The simple syntax of the python join() method looks like this:

separator.join(string)

For the multi-line strings, the join() method will take the strings and will join them using the specified separator.

 

Example of Python multi line strings using join() method

Now let us take an example and see how we can define python multi line strings using the python join() method. See the example below:

# python multi line strings using join() method
string1 =''.join(("This is",
 " going to be ",
 "multi line string"))
string2 = ''.join(("############"
" Welcone to"
" Go linux Cloud!!"))
# printing the strings
print(string1)
print(string2)

Output:

This is going to be multi line string
############ Welcone to Go linux Cloud!!

Notice that we haven't provided any separator ( it is empty), so it joins all the multiline strings together. If we want to print the multiline strings as they are, then again we have to add the line break. See the example below:

# python multi line strings using join method
string1 =''.join(("This is\n",
 "going to be \n",
 "multi line string"))
string2 = ''.join(("############\n"
" Welcone to\n"
" Go linux Cloud!!"))
# printing the strings
print(string1)
print(string2)

Output:

This is
going to be
multi line string
############
Welcone to
Go linux Cloud!

Notice that this time, we were able to successfully print the multiline strings as they are by using the escape character.

 

Summary

Sometimes we have a very long string and we want to write it into multiple lines for better code readability. Python provides various ways to create multiline strings. In this tutorial, we covered five different methods to write multiline strings in Python by taking various examples. The methods include using three double quotes, three single quotes, backslash, brackets, and using join() method. To summarize, this tutorial contains all the necessary methods and ways through which you can define a multiline string in Python.

 

Further Reading

Python String
Python docstrings
Python methods

 

Didn't find what you were looking for? Perform a quick search across GoLinuxCloud

If my articles on GoLinuxCloud has helped you, kindly consider buying me a coffee as a token of appreciation.

Buy GoLinuxCloud a Coffee

For any other feedbacks or questions you can either use the comments section or contact me form.

Thank You for your support!!

Leave a Comment