Last few tutorials have made the concepts of using a single and double quote as well as when and where to use escape character very clear to us. But, all these are the tools for single line processing, what if we want to print a multi-line paragraph. That is exactly what I will be addressing in this article. Here in this tutorial, I will show you how you can deal with multi-line string in Python Programming using triple quotation marks.
For a better understanding of this concept, you will require a working knowledge of single-line processing and escape character. Therefore I suggest you check out these tutorials first.
Single-line strings in Python Programming, and
Escape Character of Python Programming
Multi-line string in Python
The multi-line string which I will be using in this tutorial for the demonstration will be—
What’s up Internet! I’m Manish from RebellionRider.com. Keep watching these tutorials. And, you will be the master of “Python Programming” very soon.
This is a generic paragraph which I will be using to show you how to work with multi-line strings in Python. My aim will be to print this paragraph just like it is appearing right now. Seems like these simple looking sentences aren’t that simple. Let’s analyze it.
Dissection of the multi-line string
As you can see this paragraph consists of four sentences. From Python Programming perspective, each of these lines is unique in their own way. Therefore they require different processing from Python Interpreter. Let’s see how they are different?
First two sentences
What’s up Internet! I’m Manish from RebellionRider.com.
First two sentences consist of an apostrophe mark in the middle, therefore enclosing them into a pair of single quotes is not possible. This is because the rule says if a string contains punctuation marks like a single quote or an apostrophe then enclose it into a pair of double quotes.
The third sentence
Keep watching these tutorials.
The third sentence is a simple string therefore we can wrap it in either of the quotation marks, whether it’s single quotes or double quotes.
The last line
And, you will be the master of “Python Programming” very soon.
Since this sentence consists a phrase enclosed into a pair of double quotes thus we cannot enclose it into a pair of double quotes itself. This is because the second rule says – if a string contains punctuation marks like double quotes then enclose it into a pair of single quotes.
The conclusion of the dissection.
As a result of the above analysis, we can say that we can neither use single quotes nor the double quotes to enclose our paragraph. We cannot use single quotes because the first two sentences contain apostrophe mark in the middle. Furthermore, the last sentence rules out the use of double quotes.
Python Interpreter is Perpetrator.
As we know Python is an interpreted language which means Python interpreter processes and translates the program statement by statement. In simple words, as soon as we hit the enter key to write the second line we will get a syntax error. I have shown the demonstration of the same in the video tutorial, which you can check here.
Since Python is an interpreted programming language where interpreter translates the program statement by statement, the use of escape character is also not possible. Escape character can only help in bypassing the punctuation marks in single line strings.
[bctt tweet=”#Python Programming provides us a very simple way to deal with multi-line strings. ” username=”RebellionRider”]
Triple Quote for Multi-line strings in Python Programming!
Python Programming provides us a very simple way to deal with multi-line strings. To print a multi-line string in Python we use triple quotes. By triple quotes, I mean set of quotation marks enclosed within each other. These quotation marks could either be double quotes or single quotes.
Example of Triple Quote.
Let me show you, how you can use the triplet of quotation marks to print a multi-line string in Python Programming.
print(“ “ “ What’s up Internet! I’m Manish from RebellionRider.com. Keep watching these tutorials. And, you will be the master of “Python Programming” very soon. ” ” ”)
Triple Quotation and the Interpreter
Let’s understand why didn’t we get the syntax error with the above code? Once Python interpreter encounters the opening triplet of the quotation mark, it understands that the user is trying to enter a multi-line string. In multi-line string mode the interpreter halts the translation of the statement until it finds the closing triplet of the quotation mark. As a result of this, we don’t get any errors.
Can we use a triplet of single quotes instead of double quotes?
Of course, you can! You can use either single quotes or double quotes. The choice is yours. Just make sure to use the same opening and closing quotes to enclose your multi-line string.
Punctuation marks in the multi-line string
You must be wondering, do we need to decide the enclosing quotation marks according to the punctuation marks coming in the middle of the string?
No, you don’t. You can enclose your multi-line string in any quotation mark that you want. It could be a triplet of single quotes or double quotes. Any punctuation marks won’t be a problem as long as you are using triple quotation marks for enclosing your string.
Comment and tell me!
Comment and tell me what multi-line string you would use in your Python Code to practice the concept?
That’s it for this tutorial. If you still have any doubt then leave a comment or send me a message on my Facebook or Twitter. Anyway, Hope you enjoyed reading. Do make sure to share the link of this blog on your social media.
Good luck and Goodspeed!