Triple quotes is a widely used feature in Python. It is employed mainly as a string delimiter for multiline strings (also known as docstring).
In this tutorial, you will learn how to use triple quotes in Python and explore some of its usage scenarios. By the end of this tutorial, you will know how to type triple quotes in Python and will be able to apply them accordingly in your Python code.
Step 1: Understand the Types of Triple Quotes
Triple quotes can be written in two different ways in Python:
- Triple single quotes: ”’ (three consecutive single quotes)
- Triple double quotes: “”” (three consecutive double quotes)
These triple quotes can be used interchangeably, but it is essential to match the starting and ending triple quotes. For example, if you start with triple single quotes, you must end the string with triple single quotes and vice versa.
Step 2: Type Triple Quotes in Python
To type triple quotes, start by opening your preferred Python environment (e.g., Python IDE or code editor). Then, start typing out your multiline string, wrapped with either triple single quotes or triple double quotes. Here is an example:
1 2 3 4 5 |
multiline_string = '''This is a multiline string in Python. Triple quotes are used to define it.''' print(multiline_string) |
Here, you will observe that the triple single quotes are used to define a multiline string, which is then printed out. The output of this code would be:
This is a multiline string in Python. Triple quotes are used to define it.
You can achieve the same result using triple double quotes as follows:
1 2 3 4 5 |
multiline_string = """This is another multiline string in Python. Triple quotes are used to define it.""" print(multiline_string) |
Step 3: Use Triple Quotes for Docstrings
In Python, docstrings are used to provide a brief description of the purpose and functionality of functions, classes, or modules. Triple quotes are utilized to define these docstrings.
Here’s an example of using triple quotes to define a docstring for a function:
1 2 3 4 5 6 7 8 |
def add_numbers(a, b): """ This function adds two numbers and returns the result. """ return a + b result = add_numbers(3, 5) print(result) |
The output for this code would be:
8
In this example, the function add_numbers has a docstring that explains its purpose, and triple quotes are used to define the docstring.
Full Code
Here is the full code, incorporating both examples from Steps 2 and 3:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
multiline_string = '''This is a multiline string in Python. Triple quotes are used to define it.''' print(multiline_string) def add_numbers(a, b): """ This function adds two numbers and returns the result. """ return a + b result = add_numbers(3, 5) print(result) |
When you run this code, you should see the following output:
This is a multiline string in Python. Triple quotes are used to define it. 8
Conclusion
Congratulations! You have now learned how to type triple quotes in Python and discovered their primary usage for multiline strings and docstrings.
Start incorporating triple quotes into your Python projects and make them more readable and maintainable for yourself and other developers who might be working on the same Python code.