We are committed to provide service 24 X 7

Deals, Shopping, Training, Tools

How to create a long multiline string in Python?

In this python tutorial, you will learn to create a long multi-line string in Python.

Table Of Contents

There are different ways to create a long multi-line string in Python. Let’s discuss them one by one.

Advertisements

Create a long multi-line string using three double quotes

Here, we will create a multi-line string inside three double-quotes.

Syntax:

"""
long multi-line string 
"""

Example:

In this example, we will create a long string of six lines using double quotes and display it.

# A long multiline string
input_str = """At ownyourlife.com.ng, 
we are always looking to help people with high-quality content. 
We have decided to open the site for submissions.
Once your article is accepted, 
it will publish under your name.
At the end of the article, we will add the author’s profile.
"""

# Display the string
print(input_str)

Output:

At ownyourlife.com.ng, 
we are always looking to help people with high-quality content. 
We have decided to open the site for submissions.
Once your article is accepted, 
it will publish under your name.
At the end of the article, we will add the author’s profile.

Create a long multi-line string using three single quotes

Here, we will create a multi-line string inside three single quotes.

Syntax:

'''
long multi-line string 
'''

Example:

In this example, we will create a long string in six lines using single quotes and display it.

# A long multiline string
input_str = '''At ownyourlife.com.ng, 
we are always looking to help people with high-quality content. 
We have decided to open the site for submissions.
Once your article is accepted, 
it will publish under your name.
At the end of the article, we will add the author’s profile.
'''

# Display the string
print(input_str)

Output:

At ownyourlife.com.ng, 
we are always looking to help people with high-quality content. 
We have decided to open the site for submissions.
Once your article is accepted, 
it will publish under your name.
At the end of the article, we will add the author’s profile.

Create a long multi-line string using backslash

Here, we will create a multi-line string, such that the each line is ending with a backslash ( ). And we have to specify each string line inside “” by ending the line with n.

Syntax:

"line 1n"
"line 2n"
.....
.....
"line n n"

Example:

In this example, we will create a long string in 6 lines with a backslash and display it.

# A long multiline string
input_str = "At ownyourlife.com.ng,n" 
"we are always looking to help people with high-quality content.n" 
"We have decided to open the site for submissions.n"
"Once your article is accepted,n" 
"it will publish under your name.n"
"At the end of the article, we will add the author’s profile.n"


# Display the string
print(input_str)

Output:

At ownyourlife.com.ng, 
we are always looking to help people with high-quality content. 
We have decided to open the site for submissions.
Once your article is accepted, 
it will publish under your name.
At the end of the article, we will add the author’s profile.

Create a long multi-line string using brackets

Here, we will create a multi-line string such that each line is ending with n. We must keep each line inside “” and the whole string is surrounded by parenthesis.

Syntax:

(
"line 1n"
"line 2n"
.....
.....
"line n n"
)

Example:

In this example, we will create a long string in 6 lines with brackets and display it.

# A long multiline string
input_str = ("At ownyourlife.com.ng,n" 
"we are always looking to help people with high-quality content.n" 
"We have decided to open the site for submissions.n"
"Once your article is accepted,n" 
"it will publish under your name.n"
"At the end of the article, we will add the author’s profile.n")


# Display the string
print(input_str)

Output:

At ownyourlife.com.ng, 
we are always looking to help people with high-quality content. 
We have decided to open the site for submissions.
Once your article is accepted, 
it will publish under your name.
At the end of the article, we will add the author’s profile.

Create a long multi-line string using the join() function.

Here, we will create a multi-line string such that each line is ending with n. We must keep each line inside “” and the whole string is surrounded by two parentheses. Basically, the whole thing is surrounded by the join() function. Indirectly, join() takes the strings as a parameter and merge them into a single string

Syntax:

''.join((
"line 1n"
"line 2n"
.....
.....
"line n n"
))

Example:

In this example, we will create a long string in 6 lines using join() and display it.

# A long multiline string
input_str = ''.join(("At ownyourlife.com.ng,n" 
"we are always looking to help people with high-quality content.n" 
"We have decided to open the site for submissions.n"
"Once your article is accepted,n" 
"it will publish under your name.n"
"At the end of the article, we will add the author’s profile.n"))


# Display the string
print(input_str)

Output:

At ownyourlife.com.ng, 
we are always looking to help people with high-quality content. 
We have decided to open the site for submissions.
Once your article is accepted, 
it will publish under your name.
At the end of the article, we will add the author’s profile.

Summary

We have seen several ways to create a long multi-line string in Python using single or double quotes. Also, “n” is used with join(), backslash, and brackets. Based on the need, you can create your multiline string. Happy Coding.

Cart

Your Cart is Empty

Back To Shop