Python multi line strings [5 Methods Explained] | GoLinuxCloud (2023)

Table of Contents

Advertisement

Introduction to Python multi line strings

In Python, Strings arearrays 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.

ALSO READ: Python Set add() Examples [Tutorial]

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.

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 isMulti-line stringIn 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 stringsstring1 = '''This is going to be multi line string'''string2 = '''############Welcone to Go linux Cloud!!'''# printing the stringsprint(string1)print(string2)

Output:

This isgoing to bemulti line string############Welcone toGo 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

ALSO READ: Python substring Explained With Examples

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 ismulti-lineString 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 quotesstring1 = """This is going to be multi line string"""string2 = """############Welcone to Go linux Cloud!!"""# printing the stringsprint(string1)print(string2)

Output:

This isgoing to bemulti line string############Welcone toGo 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.

ALSO READ: Python static method Explained [Practical Examples]

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= bracketsstring1 = ("This is " "going to be " "multi line string")string2 = ("############" " Welcone to " " Go linux Cloud!!")# printing the stringsprint(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= bracketsstring1 = ('This is ' 'going to be ' 'multi line string')string2 = ('############'' Welcone to ' ' Go linux Cloud!!')# printing the stringsprint(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 stringsprint(string1)print(string2)

Output:

This isgoing to bemulti line string############Welcone toGo linux Cloud!!

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

Advertisement

ALSO READ: 10+ basic examples to learn Python RegEx from scratch

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 inrepresenting 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.

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 backslashstring1 ='This is'\ ' going to be ' \ 'multi line string'string2 = '############'\' Welcone to ' \' Go linux Cloud!!'# printing the stringsprint(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 backslashstring1 ="This is"\ "going to be " \ "multi line string"string2 = "############"\" Welcone to "\" Go linux Cloud!!"# printing the stringsprint(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 stringsprint(string1)print(string2)

Output:

This isgoing to bemulti line string############Welcone toGo linux Cloud!!

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

ALSO READ: Python round up practical examples [5 Methods]

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:

Advertisement

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() methodstring1 =''.join(("This is", " going to be ", "multi line string"))string2 = ''.join(("############"" Welcone to"" Go linux Cloud!!"))# printing the stringsprint(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 methodstring1 =''.join(("This is\n", "going to be \n", "multi line string"))string2 = ''.join(("############\n"" Welcone to\n"" Go linux Cloud!!"))# printing the stringsprint(string1)print(string2)

Output:

This isgoing to bemulti line string############Welcone toGo linux Cloud!

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

ALSO READ: Numpy random Examples | rand() | randint() function

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

Top Articles
Latest Posts
Article information

Author: Kimberely Baumbach CPA

Last Updated: 03/04/2023

Views: 5827

Rating: 4 / 5 (61 voted)

Reviews: 92% of readers found this page helpful

Author information

Name: Kimberely Baumbach CPA

Birthday: 1996-01-14

Address: 8381 Boyce Course, Imeldachester, ND 74681

Phone: +3571286597580

Job: Product Banking Analyst

Hobby: Cosplaying, Inline skating, Amateur radio, Baton twirling, Mountaineering, Flying, Archery

Introduction: My name is Kimberely Baumbach CPA, I am a gorgeous, bright, charming, encouraging, zealous, lively, good person who loves writing and wants to share my knowledge and understanding with you.