Skip to content

[Enhancement]: textwrap.dedent() to prettify a multiline string #328

Description

@opk12

Overview

Is it in-scope to suggest wrapping a multiline string literal inside textwrap.dedent() to keep it readable and consistently indented?

Proposal

$ cat a.py
#!/usr/bin/env python3

import textwrap

class C:
    def make_with_dedent(self):
        literal = textwrap.dedent("""\
            This is a multiline
            string literal.
        """)
        return literal

    def make_without_dedent(self):
        literal = """\
This is another multiline
string literal.
        """
        return literal

print(C().make_with_dedent())
print(C().make_without_dedent())

$ ./a.py 
This is a multiline
string literal.

This is another multiline
string literal.
        

A big thanks to alonzodemo at OpenStreetMap for teaching me the trick.

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancementNew feature or request

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions