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.
1 2 3 4 5 |
triple_single_quotes = ''' This is a multiline string using triple single quotes.''' print(triple_single_quotes) |
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.
1 2 3 4 5 |
triple_double_quotes = """ This is a multiline string using triple double quotes.""" print(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:
1 2 3 4 5 |
escaped_triple_quotes = ''' This is a multiline string with triple single quotes: \''' and triple double quotes: \"\"\".''' print(escaped_triple_quotes) |
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.
1 2 3 4 5 6 7 8 9 |
def sample_function(): """ This is a sample function that demonstrates the use of triple quotes for docstrings. """ print("Hello Triple Quotes!") help(sample_function) |
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
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 |
triple_single_quotes = ''' This is a multiline string using triple single quotes.''' print(triple_single_quotes) triple_double_quotes = """ This is a multiline string using triple double quotes.""" print(triple_double_quotes) escaped_triple_quotes = ''' This is a multiline string with triple single quotes: \''' and triple double quotes: \"\"\".''' print(escaped_triple_quotes) def sample_function(): """ This is a sample function that demonstrates the use of triple quotes for docstrings. """ print("Hello Triple Quotes!") help(sample_function) |
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.