question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Multiline string indentation

See original GitHub issue

Rule request

Thesis

Flake8 has no opinion about multiline strings at all. Everything in this example is correct:

def f():
    a = """
        text
        text
    """
    a = """
        text
        text
        """
    a = """text
        text
        """
    a = """text
        text"""
    a = """
text
text
"""
    return a

And so on.

Let’s choose the only option.

The best option

I vote for the second one:

def f():
    something = """
        text
        text
    """

Because it matches with braces style by PEP-8:

def f():
    something = (
        text,
        text,
    )

Reasoning

Consistency and readability. Let’s not allow to break block indentation in such a rough way.

Solution

The motivation behind allowing such awful things is newlines and indentation preserving in multiline strings. However, you should never rely on it because that variable could be moved in constants, function turned into a method, part of the code wrapped into with statement, and a lot more ways how indentation for statement can be changed. So, always use textwrap.dedent, textwrap.indent, and str.strip to get expected indentation and newlines for a multiline string.

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Comments:12 (10 by maintainers)

github_iconTop GitHub Comments

1reaction
sobolevncommented, Apr 28, 2020

I would say these two are different violation. The first one is the best practice, the second one is consistency.

1reaction
sobolevncommented, Apr 28, 2020

Thanks a lot!

Shall multiline strings be allowed to use directly (without assigning to a variable)?

Nope, looks quite unreadable.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Proper indentation for multiline strings? - python
Docstrings are treated specially: any indent of the first line is removed; the smallest common indent taken over all other non-blank lines is...
Read more >
Proper Indentation for Python Multiline Strings - Finxter
An alternative to using triple quotes ( ''' ) to create a multiline string is to enclose the entire string in brackets (...
Read more >
Changing the Indentation of a Multiline String - O'Reilly
When working with text, it may be necessary to change the indentation level of a block. This recipe's code takes a multiline string...
Read more >
Proper indentation for multiline strings in Python | bobbyhadz
To properly indent multiline strings, add a backslash at the end of the first line. Close the multiline string on the last line....
Read more >
Multiline string literal tips and tricks - Swift by Sundell
A few features that make Swift's multiline string literals really useful. ... We could also indent the above message, as multiline string ......
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found