What is the Use of Triple Quotes in Python?
Before discussing the use of Python triple quotes, let me ask you a question. Have you ever created multi-line strings in Python?
First, we will discuss the use of Python triple quotes, which are used in making multi-line strings. It is possible to span strings across many lines using Python triple quotes. It can also be applied to lengthy comments inside the code. Triple quotes can also contain special characters like TAB, verbatim, or NEWLINES. As the name would imply, the syntax consists of three single or double quotes placed consecutively.
Syntax
Note: Official Python documentation claims that Python triple quotes are docstrings or multi-line docstrings. Do not get confused that Python triple quotes are used only for comment purposes. The main use of using Python triple quotes is to create a multi-line and docstrings. Whenever you write a comment using the ‘#’ character, it is ignored by the Python interpreter (the interpreter does not read that line), but the multi-line string that you write inside the triple quotes is read by the Python interpreter.
Python Triple Quote for Multi-line Strings
Basically, the multi-line string will be used as a comment in the code. In the following example, we will be using a multi-line string as a comment. We will be implementing Python triple quotes using double quotes(“““ ”””) as well as single quotes (‘‘‘ ’’’).
Example 1
Output
Explanation In the above example, we are using triple quotes to create a multi-line string that acts as a comment.
Example 2 This example is the same as example 1. But here, we are taking single quotes to make a multi-line string.
Code
Output
Python Triple Quotes for String Creation
In Python, we normally create strings using double quotes (example: “Welcome to Scaler”). But string creation is possible using triple quotes, also. Also, we can create a multi-line string using triple quotes. Using normal double quotes, you cannot create a multi-line string. For that, you need to use triple quotes. Furthermore, we will cover some examples that will cover major types of string creation.
Code
Output
Explanation
Example 1: In the example 1, we are creating a multi-line string using Python triple quotes(“““ ”””).
Example 2: In the example 2, we are creating a multi-line string using single Python triple quotes(‘‘‘ ’’’).
Example 3: In the third example, we are creating a string that ignores the end of the line using the ‘\’ character.
Python Tripe Quote for Docstrings
Python documentation strings, often known as docstrings, offer a practical manner of connecting documentation to Python modules, classes, functions, and methods. It is stated in the source code that it is used to document a particular section of code, like a comment. The docstring should explain what the function does, not how it completes it, unlike standard source code comments. Following is the example of triple quotes that is used in creating docstrings.
Code
Output
Advantages of Using Python Triple Quote
- We can use triple quotes to create a multi-line string.
- Triple quotes are used to span multiple lines, and they also serve as docstrings. Docstring is basically a convenient way of associating documentation with Python modules.
- Multi-line comments can also be created using triple quotes.
- It can also be used for evaluating complex expressions, and docstrings.
Disadvantages of Using Python Triple Quote
- If you use triple quotes to comment a line, then the Python interpreter will read it as a line. This will increase interpreter time.
Learn more
If you like this article, you refer to more such interesting blogs on Scaler Topics. Following are links to some blogs that might help you!
Conclusion
- The main purpose of using triple quotes in python is to create multi-line strings and docstrings.
- We can create triple quotes using double (“““ ”””) as well as single (‘‘‘ ’’’) quotes.
- The content inside the triple quotes is read by a Python interpreter.