VOOZH about

URL: https://www.geeksforgeeks.org/python/triple-quotes-in-python/

⇱ Triple Quotes in Python - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

Triple Quotes in Python

Last Updated : 12 Jul, 2025

In Python, triple quotes let us write strings that span multiple lines, using either three single quotes (''') or three double quotes ("""). While they’re most often used in docstrings (the text that explains how code works), they also have other features that can be used in a variety of situations Example:


Output
Line 1
Line 2
Line 3

Explanation: triple quotes let you write a string across multiple lines without using newline characters (\n).

Note: Triple quotes are docstrings or multi-line docstrings and are not considered comments according to official Python documentation.

Syntax of Triple Quotes

There are two valid ways to create a triple-quoted string:

Triple single quotes:

s = '''This is

a triple-quoted

string.'''

Triple double quotes:

s = """This is

also a triple-quoted

string."""

Triple Quotes for String creation

We can also declare strings in python using triple quote. Here's an example of how we can declare string in python using triple quote. Example:


Output
<class 'str'>
<class 'str'>
<class 'str'>
I am a Geek

Explanation: Even though the strings are declared with triple quotes, they behave exactly like normal strings. The + operator concatenates them.

Triple Quotes for Docstrings

Docstrings are string literals that appear as the first statement in a function, class, or module. These are used to explain what the code does and are enclosed in triple quotes. Example:


Output
Hello, Anurag!

Explanation: In this example, the string """Greets the person with the given name.""" is the docstring for the msg function.

Accessing Docstrings

Docstrings can be accessed using the __doc__ attribute or the built-in help() function.


Output
Calculates the area of a circle given its radius.
Help on function area in module __main__:

area(radius)
 Calculates the area of a circle given its radius.

Explanation:

  • area.__doc__ returns the docstring associated with the area() function.
  • help() function displays the docstring along with the function signature.

Related articles


Comment