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:
Python
s = """Line 1
Line 2
Line 3
"""
print(s)
OutputLine 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:
Python
s1 = '''I '''
s2 = """am a """
s3 = """Geek"""
# check data type of str1, str2 & str3
print(type(s1))
print(type(s2))
print(type(s3))
print(s1 + s2 + s3)
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:
Python
def msg(name):
"""Greets the person with the given name."""
print(f"Hello, {name}!")
s = "Anurag"
msg(s)
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.
Python
def area(radius):
"""Calculates the area of a circle given its radius."""
import math
return math.pi * radius ** 2
print(area.__doc__)
help(area)
OutputCalculates 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
Similar Reads
Python Tuple Exercise Basic Tuple ProgramsPython program to Find the size of a TuplePython â Maximum and Minimum K elements in TupleCreate a list of tuples from given list having number and its cube in each tuplePython â Adding Tuple to List and vice â versaPython â Sum of tuple elementsPython â Modulo of tuple elementsP
3 min read
NumPy Tutorial - Python Library NumPy (short for Numerical Python ) is one of the most fundamental libraries in Python for scientific computing. It provides support for large, multi-dimensional arrays and matrices along with a collection of mathematical functions to operate on arrays.At its core it introduces the ndarray (n-dimens
3 min read
Multiline String in Python A sequence of characters is called a string. In Python, a string is a derived immutable data typeâonce defined, it cannot be altered. To change the strings, we can utilize Python functions like split, join, and replace.Python has multiple methods for defining strings. Single quotations (''), double
4 min read
Unpacking a Tuple in Python Tuple unpacking is a powerful feature in Python that allows you to assign the values of a tuple to multiple variables in a single line. This technique makes your code more readable and efficient. In other words, It is a process where we extract values from a tuple and assign them to variables in a s
2 min read
Python Variables In Python, variables are used to store data that can be referenced and manipulated during program execution. A variable is essentially a name that is assigned to a value. Unlike many other programming languages, Python variables do not require explicit declaration of type. The type of the variable i
6 min read