Python
multiline strings
code formatting
indentation
programming tips

Proper indentation for multiline strings?

Master System Design with Codemia

Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.

Introduction

Indenting multiline strings in Python is not just a style issue. The leading spaces inside the triple-quoted literal become part of the string unless you remove them deliberately. The right formatting depends on whether you want the string to preserve indentation exactly or whether you only want the code itself to stay readable.

Triple-Quoted Strings Preserve Whitespace

Python triple-quoted strings keep embedded newlines and leading spaces exactly as written.

python
1message = """
2    line one
3    line two
4"""
5
6print(repr(message))

Because the indentation is inside the string literal, the resulting value contains those spaces. That is often surprising when the code is nested inside a function or class definition.

The Most Common Fix: textwrap.dedent

If you want nicely indented source code without keeping the indentation in the final string, use textwrap.dedent.

python
1from textwrap import dedent
2
3message = dedent("""
4    line one
5    line two
6""").strip()
7
8print(repr(message))

This is the standard answer for multiline text embedded in indented code blocks.

Why the extra strip()? Triple-quoted strings often include a leading newline because the opening quotes sit on their own line. strip() removes that outer whitespace when you do not want it.

When Exact Indentation Is the Point

Sometimes preserving whitespace is correct. For example, if you are generating Python code, YAML, or a formatted text block where indentation has meaning, you may want the literal spaces.

python
1yaml_snippet = """items:
2  - apple
3  - banana
4"""
5
6print(yaml_snippet)

In that case, do not dedent blindly. The spaces are part of the data.

The main question is not "how should I indent multiline strings?" It is "should this string preserve indentation, or should the code indentation be ignored?"

Parenthesized Concatenation Is Often Cleaner

If you do not actually need embedded line breaks in the resulting string, implicit string concatenation inside parentheses is often cleaner than triple quotes.

python
1query = (
2    "select id, name "
3    "from users "
4    "where active = 1"
5)
6
7print(query)

This keeps the code readable without introducing unintended indentation or newlines.

It is especially good for:

  • SQL fragments
  • long error messages
  • URLs or command templates

where the final string should be one line.

Building Lines Explicitly

Another readable pattern is to build the final text from a list of lines and join them.

python
1lines = [
2    "Dear user,",
3    "",
4    "Your report is ready.",
5    "Please review the attachment.",
6]
7
8message = "\n".join(lines)
9print(message)

This approach works well when the content is structured and you want complete control over blank lines.

Docstrings Are a Special Case

Docstrings are multiline strings too, but they usually follow documentation conventions rather than generic text-formatting rules. Tools that inspect docstrings may normalize indentation, and developers often use helper utilities such as inspect.cleandoc when they need cleaned output.

python
1import inspect
2
3def example():
4    """
5    Return a friendly message.
6
7    The indentation is cleaned for introspection.
8    """
9    return "hello"
10
11
12print(repr(inspect.getdoc(example)))

This is a reminder that not all multiline strings are consumed the same way.

A Practical Style Rule

Use one of these patterns based on the actual output shape:

  • triple quotes plus dedent when you want a readable multiline literal without extra indentation
  • raw triple quotes when whitespace should be preserved exactly
  • parenthesized adjacent strings when the final result should be one line
  • list-of-lines plus join when the structure is assembled programmatically

Trying to force one style onto every use case usually produces awkward strings or awkward code.

Common Pitfalls

  • Forgetting that indentation inside triple quotes becomes part of the string value.
  • Leaving an unwanted leading newline because the opening triple quotes were placed on their own line.
  • Using triple quotes for single-line output where parenthesized concatenation would be cleaner.
  • Applying dedent to text where indentation is semantically meaningful.
  • Debugging alignment issues without checking repr, which reveals the actual spaces and newline characters.

Summary

  • Triple-quoted strings preserve leading spaces and newlines exactly as written.
  • Use textwrap.dedent when you want readable source indentation but clean output text.
  • Use parenthesized adjacent strings when the final output should stay on one line.
  • Preserve indentation only when it is part of the data, not just an artifact of code layout.
  • Check repr when multiline string whitespace is behaving differently than expected.

Course illustration
Course illustration

All Rights Reserved.