all Python 3Strings (str)term
Triple quotes
creation
Triple-quoted strings ("""...""" or '''...''') may contain line breaks and quotes with no escaping. Used for multi-line text and for docstrings.
Syntax
"""текст"""
'''текст'''
Everything inside is taken literally — including the newline right after the opening quotes and the indentation of the surrounding code — so multi-line text written inside a function usually carries leading spaces on every line. A trailing backslash on the first line swallows the initial newline, and textwrap.dedent() strips the indentation. A triple-quoted string becomes a docstring only when it is the first statement of a module, class or function; anywhere else it is just a value that is computed and thrown away.
Examples
s = """строка 1\nстрока 2"""
print(len(s)) # → 17
def foo():
"""Докстринг функции."""
pass
print(foo.__doc__) # → Докстринг функции.
s2 = """можно \"кавычки\" внутри"""
print("кавычки" in s2) # → True
print(type(s2)) # → <class 'str'>
See also
Open in the full glossarydocs.python.orgReport an inaccuracy