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.

Create a long multi-line string using three double quotes

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

Syntax:

Advertisements
"""
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 thispointer.com, 
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 thispointer.com, 
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 thispointer.com, 
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 thispointer.com, 
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 1\n"\
"line 2\n"\
.....
.....
"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 thispointer.com,\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 thispointer.com, 
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 1\n"
"line 2\n"
.....
.....
"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 thispointer.com,\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 thispointer.com, 
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 1\n"
"line 2\n"
.....
.....
"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 thispointer.com,\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 thispointer.com, 
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.

Pandas Tutorials -Learn Data Analysis with Python

   

Are you looking to make a career in Data Science with Python?

Data Science is the future, and the future is here now. Data Scientists are now the most sought-after professionals today. To become a good Data Scientist or to make a career switch in Data Science one must possess the right skill set. We have curated a list of Best Professional Certificate in Data Science with Python. These courses will teach you the programming tools for Data Science like Pandas, NumPy, Matplotlib, Seaborn and how to use these libraries to implement Machine learning models.

Checkout the Detailed Review of Best Professional Certificate in Data Science with Python.

Remember, Data Science requires a lot of patience, persistence, and practice. So, start learning today.

Join a LinkedIn Community of Python Developers

Leave a Comment

Your email address will not be published.

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

Scroll to Top