EDUCBA Logo

EDUCBA

MENUMENU
  • Explore
    • EDUCBA Pro
    • PRO Bundles
    • Featured Skills
    • New & Trending
    • Fresh Entries
    • Finance
    • Data Science
    • Programming and Dev
    • Excel
    • Marketing
    • HR
    • PDP
    • VFX and Design
    • Project Management
    • Exam Prep
    • All Courses
  • Blog
  • Enterprise
  • Free Courses
  • Log in
  • Sign Up
Home Software Development Software Development Tutorials Python String Tutorial Python Multiline String
 

Python Multiline String

Abhilasha Chougule
Article byAbhilasha Chougule
EDUCBA
Reviewed byRavi Rathore

Updated June 23, 2023

Python Multiline String

 

 

Introduction to Python Multiline String

In Python, the string is a sequence of characters, and to declare a string, we use single quotes that are used for declaring a single-line string. In this multiline string in Python is defined using triple quotes such as “““ ”””. Therefore any string or any literals between these triple quotes is considered as a string. In the Python program, multiline strings are mainly used to specify comments describing the program’s working. In Python, the multiline string is the most recommended way to represent a string. In Python, when we are using a string with more than one line and if they have a newline character, then we use triple quotes, else there are different ways to represent a string with no newline character.

Watch our Demo Courses and Videos

Valuation, Hadoop, Excel, Mobile Apps, Web Development & many more.

Working of Python Multiline String with Examples

In this article, we will see how to declare or define a multiline string in Python. We should note that if we are using a lengthy string in the Python program, which is mainly used for declaring comments in programs, and this lengthy string with a newline character, we can declare it using triple quotes ( “““ ””” ) or three single quotes( ‘ ‘ ‘ ). If we are trying to declare a lengthy string without a newline character, then we cannot use triple quotes instead; there are different ways of declaring such a string.

Firstly, we will see how to declare a multiline string in Python having a newline character using triple quotes along with examples.

Example:

Let us consider we want to declare a long string in the program. Then let us see how we can declare it using single and double triple quotes.

print("Program to demonstrate double and single triple quotes for multiline string")
print("\n")
double_qt = """Educba Training Institue,
provides different programming langauges,
in Chennai India."""
print(double_qt)
print("\n")
single_qt = '''Educba Training Institue,
provides different programming langauges,
in Chennai India.'''
print(single_qt)

Output:

Python Multiline String-1.1

In the above program, we can see how to declare a multiline string using double and single triple quotes. In the above program, we have written each and every line in the newline.

Now we will find different ways to declare multiline strings in Python. Let us see each of them, along with examples. Therefore different ways to declare multiline strings are as follows:

1. Using brackets

This is another way to declare a multiline string in Python when there is no newline in the string using brackets for declaring a multiline string. Let us see it in the example below:

Example:

print("Program to demonstrate to declaring multiline string using brackets")
print("\n")
brkt_str = ("EDUCBA is a leading global provider of skill based online education."
"EDUCBA is currently catering more than 500,000+ learners across 40+ countries.")
print(brkt_str)

Output:

Python Multiline String-1.2

In the above program, we can see we use brackets to declare a multiline string. In this, we can note that the output will print the string declared without newline character as there is no newline character in the given string. Therefore we use brackets to split a string into multiple lines.

2. Using backslash ( \ )

In this option, we use a backslash ( \ ) for a multiline string in Python that does not contain a newline character in the given string. The option of using backslash is used for declaring string to print multiple lines. Let us demonstrate in the below example.

Example:

print("Program to demonstrate multiline string using backslash")
print("\n")
bck_str = "Educba Training Institue." \
    "This institute is highly recommended for online programming courses and " \
    "this course also provide certificates."
print("The multiline string is given as follows:")
print("\n")
print(bck_str)

Output:

Output-1.3

In the above program, we can see that for each string at the end, we have given a backslash ( \ ), and the last string of the multiline string does not have a backslash. Therefore we can see that the given string does not have any newline character, and therefore using a backslash, we can print the multiline string.

3. Using join()

In this option, we use the string join() function of Python to split up multiple strings used for printing multiline strings. So in the above two options, there was a condition to check for spaces when we use either brackets or backslash because if we have a very long string, then it would be difficult if we miss out space when we use these two options, but this problem can be overcome using string join() method. Let us demonstrate it below with an example.

Example:

print("Program to demonstrate multiline string using join()")
print("\n")
jn_str = ' '.join(("Educba Training Institue",
    "This institute is highly recommended for online programming courses and",
    "this course also provide certificates."))
print("The given string is as follows:")
print("\n")
print(jn_str)

Output:

Output-1.4

In the above program, we can see we are using join() function of string. In this, we can notice that we are passing the multiline string to this function. Therefore it prints the multiple lines of string using the join() function, and we can note that we are using space and then using the join() function to it. Therefore this is easy when we are using lengthy strings and there is no need for a space check in the string when we join() function of the string in Python.

Conclusion

In this article, we conclude that a multiline string in Python is to print multiple lines of string.  In this article, we saw when the string has newline characters, and then we have to use triple quotes. If there are no newline characters, we have discussed various ways for declaring multiline string, such as using brackets, backslash, and join() functions of string in Python.

Recommended Articles

This is a guide to Python Multiline String. Here we also discuss the introduction and working of Python multiline string along with a different example and its code implementation. You may also have a look at the following articles to learn more –

  1. Python wait()
  2. Python StopIteration
  3. Python NameError
  4. Python Unicode Error

Primary Sidebar

Footer

Follow us!
  • EDUCBA FacebookEDUCBA TwitterEDUCBA LinkedINEDUCBA Instagram
  • EDUCBA YoutubeEDUCBA CourseraEDUCBA Udemy
APPS
EDUCBA Android AppEDUCBA iOS App
Blog
  • Blog
  • Free Tutorials
  • About us
  • Contact us
  • Log in
Courses
  • Enterprise Solutions
  • Free Courses
  • Explore Programs
  • All Courses
  • All in One Bundles
  • Sign up
Email
  • [email protected]

ISO 10004:2018 & ISO 9001:2015 Certified

© 2025 - EDUCBA. ALL RIGHTS RESERVED. THE CERTIFICATION NAMES ARE THE TRADEMARKS OF THEIR RESPECTIVE OWNERS.

EDUCBA

*Please provide your correct email id. Login details for this Free course will be emailed to you
Loading . . .
Quiz
Question:

Answer:

Quiz Result
Total QuestionsCorrect AnswersWrong AnswersPercentage

Explore 1000+ varieties of Mock tests View more

EDUCBA

*Please provide your correct email id. Login details for this Free course will be emailed to you
EDUCBA
Free Software Development Course

Web development, programming languages, Software testing & others

By continuing above step, you agree to our Terms of Use and Privacy Policy.
*Please provide your correct email id. Login details for this Free course will be emailed to you
EDUCBA

*Please provide your correct email id. Login details for this Free course will be emailed to you

EDUCBA Login

Forgot Password?

🚀 Limited Time Offer! - 🎁 ENROLL NOW