views:

380

answers:

6

Due to the sheer annoyance of figuring out what to Google, I've decided to risk what any reputation I had to ask this rather simple 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."
+3  A: 

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.")
recursive
+1  A: 

With an \ you can expand statments to multiple lines

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

should work.

Ikke
+5  A: 

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.

retracile
+1  A: 

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.

ZombieSheep
I totally agree. There is a similar Java style rule that has become obsolete too (IMHO).
Iker Jimenez
Yes I agree, however It's been racking my head how I would abide to it in this particular example. I always try to keep classes, methods to < 80 characters, however I'd say a string like this has no effect other than perhaps a negative one.
day_trader
+1 for valid argument :)
day_trader
You also need to weigh your personal preference against the community-wide coding standard. You want new people to be able to come in and be comfortable with the code formatting from day one.
retracile
I know for my own self, I tend to stick to the 80 character limit just because I still do most of my coding in IDLE and I don't like the way it handles horizontal scrolling. (No scroll bar)
Tofystedeth
@retracile - yes, you do. I am not saying "You must ignore the guidance", rather suggesting that in some cases the guidance is not necessarily there for the good of the community. I wasn't aware of IDLE's restrictions (as posted by Tofystedeth) but in that instance there is a striong argument for following the convention.
ZombieSheep
+9  A: 

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."
Michael Dunn
This is why I felt like an idiot posting the question. Cheers.
day_trader
+8  A: 

Also, because neighboring string constants are automatically concatenated, you can code it like this too:

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.")

Update:

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".

Todd
+1 agree with everything
kaizer.se