The Definitive Guide to Python Triple Quotes: Multiline, Quotes Inside and Docstring
- October 27, 2025
- 9 min read
- Python programming
Table of Contents
Triple quotes (""" or ''') in Python are deceptively simple — yet incredibly powerful. They let you:
- Write multiline strings with real line breaks
- Embed both single (
') and double (") quotes naturally - Create docstrings for functions, classes, and modules
- Combine with f-strings for clean, dynamic text
Let’s explore how to use them effectively with real-world examples.
​
1. Writing Multiline Strings
​
Traditional way: \n and concatenation
You can break lines using \n:
1text = "Hello! My name is Python.\nI was designed for readability.\nThis code, however, is not."
2print(text)
But this becomes hard to read for longer strings. You might split and concatenate:
1text = "This is line one.\n" + \
2 "This is line two.\n" + \
3 "This is line three."
Readable? Could be better.
​
Triple quotes to the rescue
Triple quotes make it simple:
1text = """Hello, I am a multiline string.
2I automatically capture
3all the new lines,
4
5including the blank one above."""
6print(text)
Output preserves the line breaks — no manual \n required.
Note
By starting the string immediately after the opening quotes ("""), we suppress the leading newline character that would otherwise be present.
However, indentation inside functions adds spaces to your string:
1def show_message():
2 msg = """Line A
3 Line B
4 Line C"""
5 return msg
6
7print(show_message())
Output:
Line A
Line B
Line C
To control indentation, you can:
- Add a backslash (
\) after the opening triple quotes to skip the initial blank line. - Or use
textwrap.dedent()to clean leading spaces:
1import textwrap
2
3text = """\
4 Clean line one
5 Clean line two
6"""
7print(textwrap.dedent(text))
Output:
Clean line one
Clean line two
Newsletter
Subscribe to our newsletter and stay updated.
​
2. Embedding Quotes Without Escaping
When your string contains both ' and ", escaping can get messy:
1s = "He said, \"I can't do that.\""
With triple quotes, it’s clean and clear:
1s = """He said, "I can't do that.""""
Both triple single quotes and triple double quotes work:
1s2 = '''She asked, "Isn't this fun?"'''
​
Raw triple-quoted strings
Triple quotes are not raw strings by default. Escape sequences like \n and \t still apply.
For literal output, use a raw triple-quoted string:
1s = r"""This shows \n and \\literally.
2Newlines as well."""
3print(s)
Output:
This shows \n and \\ literally.
Newlines as well.
Newsletter
Subscribe to our newsletter and stay updated.
​
3. Combining Triple Quotes with F-Strings
You don’t have to choose between features! You can combine the best of modern Python: f-strings (formatted string literals) with the flexibility of triple quotes.
Just prefix the opening triple quotes with an f:
1user_name = "Alex"
2login_count = 14
3role = "Admin"
4
5# Multiline F-String
6welcome_message = f"""
7Hello, **{user_name}**!
8You have successfully logged in {login_count} times.
9Your current role is: **{role}**.
10
11Thank you for using our service.
12"""
13print(welcome_message)
Output:
Hello, **Alex**!
You have successfully logged in 14 times.
Your current role is: **Admin**.
Thank you for using our service.
This technique is ideal for emails, templates, or CLI messages — anywhere you want both structure and variable interpolation.
Warning
When using multiline f-strings inside a function or class method, be especially mindful of indentation.
The whitespace leading up to the text will be included in the resulting string.
If this is undesirable, you may need to use textwrap.dedent() to clean it up.
Newsletter
Subscribe to our newsletter and stay updated.
​
4. Docstrings: Triple Quotes for Documentation
​
What is a docstring?
A docstring is a string literal that appears right after a function, class, or module definition.
It documents what that object does — and unlike a comment, it’s stored at runtime.
Example:
1def add(a, b):
2 """Return the sum of a and b."""
3 return a + b
You can access it via:
1print(add.__doc__)
2# Output: Return the sum of a and b.
Or use the built-in help() function:
1help(add)
​
Style guidelines (PEP 257)
As per PEP 257 – Docstring Conventions:
- Use triple double quotes (
""") for all docstrings. - Start with a one-line summary.
- Leave a blank line before detailed descriptions.
- Indent to match your code block.
For further resources on docstring style guidelines, refer to my article on Python useful resources.
Example:
1def multiply(a, b):
2 """
3 Multiply two numbers a and b.
4
5 Parameters:
6 a (int or float): First operand
7 b (int or float): Second operand
8
9 Returns:
10 int or float: Product of a and b
11 """
12 return a * b
​
Docstrings vs Comments
Docstrings ≠ comments.
Triple-quoted strings inside code blocks are executed as string literals, not ignored like # comments:
1def foo():
2 """Valid docstring."""
3 """This is not a comment — just an unused string."""
4 return 42
Only the first triple-quoted string ("""Valid docstring.""") is treated as the function’s docstring.
The second triple-quoted string is just an unused string literal expression.
Python evaluates it but then discards the result, though it still slightly increases the bytecode size compared to a true # comment.
For comments, always use #.
Warning
Docstrings Cannot Be F-strings
Do not use an f-string (e.g., f"""...""") as a docstring. While triple quotes combine with f-strings for regular content, Python will not recognize a formatted string as a legitimate docstring.
It will be treated as an unused string literal, and tools like help() and documentation generators will fail to find it.
Docstrings must be plain string literals (including raw strings).
Newsletter
Subscribe to our newsletter and stay updated.
​
5. Extra Tips and Best Practices
​
Implicit string concatenation
Python automatically joins adjacent string literals:
1s = ("Line 1\n"
2 "Line 2\n"
3 "Line 3")
This is neat for controlled formatting without using triple quotes.
​
Mind your trailing newline
If you end a triple-quoted string with a blank line, it includes a trailing \n.
Use repr() to check if you’re not sure.
1s = """Line one
2Line two
3"""
4print(repr(s))
Output:
'Line one\nLine two\n'
​
Module and class docstrings
Triple quotes also work for module-level and class documentation:
1"""This module handles user authentication."""
2
3class Auth:
4 """Authentication and session management."""
Newsletter
Subscribe to our newsletter and stay updated.
​
6. Quick Reference Cheat Sheet
| Feature | Syntax | Purpose / Notes |
|---|---|---|
| Multiline string | """line1\nline2""" |
Includes actual newlines |
| Both quotes inside | """He said, "I can't."""" |
No escaping |
| Raw triple-quoted | r"""literal \n""" |
Backslashes not interpreted |
| Multiline f-string | f"""Hello {name}!""" |
Variable interpolation |
| Docstring | After def, class, or module |
Access via __doc__ or help() |
| Suppress first newline | """\ |
Avoid leading blank line |
| Clean indentation | textwrap.dedent() |
Strip leading spaces |
| Implicit concat | "A" "B" |
Joined at compile time |
Newsletter
Subscribe to our newsletter and stay updated.
​
Final Thoughts
Triple quotes are one of Python’s most elegant features — they make your code more readable, maintainable, and expressive.
At their core, triple-quoted strings are semantically identical to single or double-quoted strings, with the crucial difference that they allow literal, unescaped line breaks and internal quotes.
- Use them for multiline strings, quotes in strings and docstrings.
- Combine with f-strings for dynamic content.
- Be aware of indentation and trailing newlines.
Once you master them, you’ll write cleaner, more Pythonic code — the way it’s meant to be.