Multiline Strings in Python

Learn how to create multiline strings in Python using triple quotes, how Python handles line breaks, and when to use multiline strings for docstrings and text blocks.

5 min read

Multiline strings in Python are created with triple quotes, and they let you write text that spans multiple lines directly in your source code without inserting the \n escape sequence at every line break. Instead of writing one long string full of embedded newlines, you press Enter between the opening and closing quotes exactly where you want line breaks to appear in the resulting text. The Python interpreter preserves those line breaks as newline characters, and the string contains the same line structure that you see in your editor.

Triple-quoted strings are not a separate type from regular strings; they produce the same str objects and support all the same methods, operations, and behaviors. The triple-quote syntax is simply a more convenient way to write certain kinds of string literals, and you choose it when the content suggests it: multi-paragraph text, blocks of formatted output, email templates, SQL queries, ASCII art, and documentation strings that describe your code to other developers or to yourself six months later.

This article completes the set of foundational string-creation topics in this section. If you have read the earlier articles on creating strings and escape characters, you already know how single and double quotes work and how backslash escapes let you include special characters. Triple quotes remove the need for both quoting tricks and escape sequences when your text is long, contains both types of quotes, or needs to preserve its line structure.

How triple quotes preserve line breaks

When you write a string using three consecutive single quotes or three consecutive double quotes, Python reads everything between the opening and closing quotes as literal string content. This includes every line break you typed, every space used for indentation, and every empty line. The Python interpreter inserts a newline character at each line break and includes all the whitespace exactly as it appears in the source code.

pythonpython
message = """Hello,
This is a multiline
string in Python."""
 
print(message)

The output will show three lines because the source code contains two line breaks inside the quotes. The first line contains "Hello,", the second line contains "This is a multiline", and the third line contains "string in Python." No escape sequences were needed, and the structure of the string in the editor directly matches the structure of the printed output.

This transparency between source formatting and runtime content is both the strength and the potential pitfall of triple-quoted strings. If you indent the lines of the string to match the indentation of the surrounding code, that indentation becomes part of the string content and will appear in the output. The standard library's textwrap.dedent() function can remove leading whitespace that exists only for code formatting purposes, and it is commonly used with triple-quoted strings that are embedded inside indented code blocks.

Controlling the first and last newline

A common subtlety with triple-quoted strings is that the very first character after the opening quotes and the very last character before the closing quotes matter. If you press Enter immediately after the opening quotes, the string starts with a newline character, which means the first line of the output will be blank. To avoid this leading blank line, place a backslash immediately after the opening quotes. The backslash escapes the newline that follows, removing it from the string content.

Similarly, the position of the closing quotes determines whether the string ends with a trailing newline. If you place the closing quotes on their own line, the string includes a newline at the end. If you place them immediately after the last character of text, there is no trailing newline. Which you choose depends on what you intend to do with the string: a trailing newline is appropriate for a complete block of text destined for a file, while no trailing newline may be preferred when the string will be embedded inside a larger piece of output.

Triple-quoted strings are also the standard way to write docstrings in Python. A docstring is a string literal that appears as the first statement in a module, function, class, or method definition. Python's help() function and documentation generation tools read docstrings automatically to produce human-readable documentation. The convention is to use triple double quotes, write a concise summary on the first line, leave a blank line, and then add any additional detail. This structure is recognized across the Python ecosystem and by every major Python-aware editor and IDE.

Rune AI

Rune AI

Key Insights

  • Triple quotes (''' or """) create strings that can span any number of lines without escape characters.
  • Line breaks you type between the quotes become
    characters in the resulting string.
  • Use a backslash after the opening quotes to avoid a leading blank line in the string.
  • Triple-quoted strings are the standard way to write docstrings for modules, functions, and classes.
  • Triple quotes are also useful for strings containing both single and double quote characters without escaping.
RunePowered by Rune AI

Frequently Asked Questions

How do I create a string that spans multiple lines in Python?

Use triple quotes, either three double quotes or three single quotes. Everything between the opening and closing triple quotes becomes part of the string, including line breaks, indentation, and empty lines. For example, a string that contains three lines of text with line breaks between them is written by pressing Enter between the lines inside the triple quotes in your source code.

How do I avoid a leading blank line in a triple-quoted string?

Place a backslash immediately after the opening triple quotes. The backslash escapes the newline that follows, so the string starts on the second line of the source rather than including a blank first line. Write """\ text here""" instead of """ text here""". This pattern is common in docstrings and formatted output where you want the text to start immediately without a blank line at the top.

Are triple-quoted strings only for multiline text?

No. Triple-quoted strings can span a single line too, and they are often used for strings that contain both single and double quote characters so that neither type needs escaping. They are also used for docstrings, which are string literals placed at the start of modules, functions, classes, and methods to document what that code element does. The triple-quote syntax works regardless of whether the string actually contains line breaks.

Conclusion

Triple-quoted strings give you a clean way to embed blocks of text directly in your Python source code without escaping quote characters or manually inserting newline escape sequences. They are the standard choice for docstrings, multi-paragraph text, ASCII art, email templates, and any situation where preserving line breaks and avoiding quote-escaping clutter makes your code more readable.