How to write very long string that conforms with PEP8 and prevent E501

Question:

As PEP8 suggests keeping below the 80 column rule for your python program, how can I abide to that with long strings, i.e.

s = "this is my really, really, really, really, really, really, really long string that I'd like to shorten."

How would I go about expanding this to the following line, i.e.

s = "this is my really, really, really, really, really, really" + 
    "really long string that I'd like to shorten."
Asked By: Federer

||

Answers:

Backslash:

s = "this is my really, really, really, really, really, really" +  
    "really long string that I'd like to shorten."

or wrap in parens:

s = ("this is my really, really, really, really, really, really" + 
    "really long string that I'd like to shorten.")
Answered By: recursive

With a you can expand statements to multiple lines:

s = "this is my really, really, really, really, really, really" + 
"really long string that I'd like to shorten."

should work.

Answered By: Ikke

You lost a space, and you probably need a line continuation character, ie. a .

s = "this is my really, really, really, really, really, really" +  
    " really long string that I'd like to shorten."

or even:

s = "this is my really, really, really, really, really, really"  
    " really long string that I'd like to shorten."

Parens would also work instead of the line continuation, but you risk someone thinking you intended to have a tuple and had just forgotten a comma. Take for instance:

s = ("this is my really, really, really, really, really, really"
    " really long string that I'd like to shorten.")

versus:

s = ("this is my really, really, really, really, really, really",
    " really long string that I'd like to shorten.")

With Python’s dynamic typing, the code may run either way, but produce incorrect results with the one you didn’t intend.

Answered By: retracile

I think the most important word in your question was “suggests”.

Coding standards are funny things. Often the guidance they provide has a really good basis when it was written (e.g. most terminals being unable to show > 80 characters on a line), but over time they become functionally obsolete, but still rigidly adhered to. I guess what you need to do here is weigh up the relative merits of “breaking” that particular suggestion against the readability and mainatinability of your code.

Sorry this doesn’t directly answer your question.

Answered By: ZombieSheep

Implicit concatenation might be the cleanest solution:

s = "this is my really, really, really, really, really, really," 
    " really long string that I'd like to shorten."

Edit On reflection I agree that Todd’s suggestion to use brackets rather than line continuation is better for all the reasons he gives. The only hesitation I have is that it’s relatively easy to confuse bracketed strings with tuples.

Answered By: Michael Dunn

Since neighboring string constants are automatically concatenated, you can code it like this:

s = ("this is my really, really, really, really, really, really, "  
     "really long string that I'd like to shorten.")

Note no plus sign, and I added the extra comma and space that follows the formatting of your example.

Personally I don’t like the backslashes, and I recall reading somewhere that its use is actually deprecated in favor of this form which is more explicit. Remember "Explicit is better than implicit."

I consider the backslash to be less clear and less useful because this is actually escaping the newline character. It’s not possible to put a line end comment after it if one should be necessary. It is possible to do this with concatenated string constants:

s = ("this is my really, really, really, really, really, really, " # comments ok
     "really long string that I'd like to shorten.")

I used a Google search of "python line length" which returns the PEP8 link as the first result, but also links to another good StackOverflow post on this topic: "Why should Python PEP-8 specify a maximum line length of 79 characters?"

Another good search phrase would be "python line continuation".

Answered By: Todd

I tend to use a couple of methods not mentioned here for specifying large strings, but these are for very specific scenarios. YMMV…

  • Multi-line blobs of text, often with formatted tokens (not quite what you were asking, but still useful):

    error_message = '''
    I generally like to see how my helpful, sometimes multi-line error
    messages will look against the left border.
    '''.strip()
    
  • Grow the variable piece-by-piece through whatever string interpolation method you prefer:

    var = 'This is the start of a very,'
    var = f'{var} very long string which could'
    var = f'{var} contain a ridiculous number'
    var = f'{var} of words.'
    
  • Read it from a file. PEP-8 doesn’t limit the length of strings in a file; just the lines of your code. 🙂

  • Use brute-force or your editor to split the string into managaeble lines using newlines, and then remove all newlines. (Similar to the first technique I listed):

    foo = '''
    agreatbigstringthatyoudonotwanttohaveanyne
    wlinesinbutforsomereasonyouneedtospecifyit
    verbatimintheactualcodejustlikethis
    '''.replace('n', '')
    
Answered By: Larold

I’ve used textwrap.dedent in the past. It’s a little cumbersome so I prefer line continuations now but if you really want the block indent, I think this is great.

Example Code (where the trim is to get rid of the first ‘n’ with a slice):

import textwrap as tw
x = """
       This is a yet another test.
       This is only a test"""
print(tw.dedent(x))

Explanation:

dedent calculates the indentation based on the white space in the first line of text before a new line. If you wanted to tweak it, you could easily reimplement it using the re module.

This method has limitations in that very long lines may still be longer than you want in which case other methods that concatenate strings is more suitable.

Answered By: MrMas

These are all great answers, but I couldn’t find an editor plugin that would help me with editing “implicitly concatenated” strings, so I wrote a package to make it easier on me.

On pip (install paragraphs) if whoever’s wandering this old thread would like to check it out. Formats multi-line strings the way html does (compress whitespace, two newlines for a new paragraph, no worries about spaces between lines).

from paragraphs import par


class SuddenDeathError(Exception):
    def __init__(self, cause: str) -> None:
        self.cause = cause

    def __str__(self):
        return par(
            f""" Y - e - e - e - es, Lord love you! Why should she die of
            {self.cause}? She come through diphtheria right enough the year
            before. I saw her with my own eyes. Fairly blue with it, she
            was. They all thought she was dead; but my father he kept ladling
            gin down her throat till she came to so sudden that she bit the bowl
            off the spoon. 

            What call would a woman with that strength in her have to die of
            {self.cause}? What become of her new straw hat that should have
            come to me? Somebody pinched it; and what I say is, them as pinched
            it done her in."""
        )


raise SuddenDeathError("influenza")

becomes …

__main__.SuddenDeathError: Y - e - e - e - es, Lord love you! Why should she die of influenza? She come through diphtheria right enough the year before. I saw her with my own eyes. Fairly blue with it, she was. They all thought she was dead; but my father he kept ladling gin down her throat till she came to so sudden that she bit the bowl off the spoon.

What call would a woman with that strength in her have to die of influenza? What become of her new straw hat that should have come to me? Somebody pinched it; and what I say is, them as pinched it done her in.

Everything lines up easily with (Vim) ‘gq’

Answered By: Shay

Available options:

  • backslash: "foo" "bar"
  • plus sign followed by backslash: "foo" + "bar"
  • brackets:
    • ("foo" "bar")
    • brackets with plus sign: ("foo" + "bar")
    • PEP8, E502: the backslash is redundant between brackets

Avoid

Avoid brackets with comma: ("foo", "bar") which defines a tuple.


>>> s = "a" 
... "b"
>>> s
'ab'
>>> type(s)
<class 'str'>
>>> s = "a" + 
... "b"
>>> s
'ab'
>>> type(s)
<class 'str'>
>>> s = ("a"
... "b")
>>> type(s)
<class 'str'>
>>> print(s)
ab
>>> s = ("a",
... "b")
>>> type(s)
<class 'tuple'>
>>> s = ("a" + 
... "b")
>>> type(s)
<class 'str'>
>>> print(s)
ab
>>> 
Answered By: marcanuy

If you must insert a long string literal and want flake8 to shut up, you can use it’s shutting up directives. For example, in a testing routine I defined some fake CSV input. I found that splitting it over more lines that it had rows would be mightily confusing, so I decided to add a # noqa: E501 as follows:

csv_test_content = """"STATION","DATE","SOURCE","LATITUDE","LONGITUDE","ELEVATION","NAME","REPORT_TYPE","CALL_SIGN","QUALITY_CONTROL","WND","CIG","VIS","TMP","DEW","SLP","AA1","AA2","AY1","AY2","GF1","MW1","REM"
"94733099999","2019-01-03T22:00:00","4","-32.5833333","151.1666666","45.0","SINGLETON STP, AS","FM-12","99999","V020","050,1,N,0010,1","22000,1,9,N","025000,1,9,9","+0260,1","+0210,1","99999,9","24,0000,9,1",,"0,1,02,1","0,1,02,1","01,99,1,99,9,99,9,99999,9,99,9,99,9","01,1","SYN05294733 11/75 10502 10260 20210 60004 70100 333 70000="
"94733099999","2019-01-04T04:00:00","4","-32.5833333","151.1666666","45.0","SINGLETON STP, AS","FM-12","99999","V020","090,1,N,0021,1","22000,1,9,N","025000,1,9,9","+0378,1","+0172,1","99999,9","06,0000,9,1",,"0,1,02,1","0,1,02,1","03,99,1,99,9,99,9,99999,9,99,9,99,9","03,1","SYN04294733 11/75 30904 10378 20172 60001 70300="
"94733099999","2019-01-04T22:00:00","4","-32.5833333","151.1666666","45.0","SINGLETON STP, AS","FM-12","99999","V020","290,1,N,0057,1","99999,9,9,N","020000,1,9,9","+0339,1","+0201,1","99999,9","24,0000,9,1",,"0,1,02,1","0,1,02,1",,"02,1","SYN05294733 11970 02911 10339 20201 60004 70200 333 70000="
"94733099999","2019-01-05T22:00:00","4","-32.5833333","151.1666666","45.0","SINGLETON STP, AS","FM-12","99999","V020","200,1,N,0026,1","99999,9,9,N","000100,1,9,9","+0209,1","+0193,1","99999,9","24,0004,3,1",,"1,1,02,1","1,1,02,1","08,99,1,99,9,99,9,99999,9,99,9,99,9","51,1","SYN05294733 11/01 82005 10209 20193 69944 75111 333 70004="
"94733099999","2019-01-08T04:00:00","4","-32.5833333","151.1666666","45.0","SINGLETON STP, AS","FM-12","99999","V020","070,1,N,0026,1","22000,1,9,N","025000,1,9,9","+0344,1","+0213,1","99999,9","06,0000,9,1",,"2,1,02,1","2,1,02,1","04,99,1,99,9,99,9,99999,9,99,9,99,9","02,1","SYN04294733 11/75 40705 10344 20213 60001 70222="
"""  # noqa: E501
Answered By: gerrit

Using black [https://github.com/psf/black] I format it like this.

   help=f"""filters, lista de filtros para cargar las base de conocimiento.
   Pueden mandarse solo algunos filtros ya que no son obligatorios,
   por ejemplo, si no se manda sts, se cargarán todos las bases de todos los estados.""",
Answered By: josue
message = f"Variable : child({type(child)}) -> is not of"
        " type Node."

This syntax worked for me. Note the indentation of the second statement, it should be indented correctly.

Categories: questions Tags: , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.