How To Print Triple Quotes In Python

In this tutorial, we will learn how to print triple quotes in Python programming language. Triple quotes are used to represent multiline strings where both SINGLE and DOUBLE quotes can be used as triple quotes. Python allows the use of triple quotes to help in better formatting and readability of the code. Let’s see how we can print triple quotes in Python.

Step 1: Using Single Triple Quotes

You can use triple single quotes (”’) to define a multiline string in Python. Let’s see an example of how to print a multiline string.

Here is the output of the above code:

This is a multiline
string using triple
single quotes.

Step 2: Using Double Triple Quotes

Similarly, you can use triple double quotes (“””) to define a multiline string in Python. Let’s see an example using triple double quotes.

Here is the output of the above code:

This is a multiline
string using triple
double quotes.

Step 3: Escaping Triple Quotes in a Multiline String

There are situations where you want to include triple quotes as part of your string. In such cases, you can use the backslash () to escape the triple quotes. Let’s see an example:

Here is the output of the above code:

This is a multiline string
with triple single quotes: '''
and triple double quotes: """.

Step 4: Using Triple Quotes for Docstrings

Triple quotes are widely used to write docstrings in Python. A docstring is a string literal that occurs as the first statement in a module, function, class, or method definition. It provides a convenient way to associate documentation with the code.

Here is the output of the above code:

Help on function sample_function in module __main__:

sample_function()
    This is a sample function
    that demonstrates the use
    of triple quotes for docstrings.

Full Code

Conclusion

In this tutorial, we have learned how to print triple quotes in Python using single and double triple quotes. We’ve also covered how to escape triple quotes in a multiline string and how triple quotes are used in docstrings. Triple quotes provide a convenient method for creating and documenting multiline strings in Python.