You can escape the newline and the end of a line by typing "\" as the
last character, but I think this is ugly and error prone and I avoid
it whenever I can -- which is almost always.

There are two syntax rules of Python that are not so well-known that
help avoiding the "\":

- Whenever you open a parenthesis, brace or bracket, every newline is
considered as plain space by the parser. Very often, adding
parenthesis around an expression does not change it's meaning, so
that's one way to make one logical line span multiple physical lines
without resorting to "\".

- Two or more string literals with no intervening tokens except
whitespace are parsed as a single string literal.

Putting those two rules together, the result is that this snippet:

text = ('a bb ccc dddd eeeee '
           'ffffff ggggggg hhhhhhhh')

Is exactly the same as this one, as far as the Python interpreter is concerned:

text = 'a bb ccc dddd eeeee ffffff ggggggg hhhhhhhhhh'

But note that I appended a trailing space to the first string literal,
otherwise the parser would see this:

text = 'a bb ccc dddd eeeeeffffff ggggggg hhhhhhhhhh'


HTH.

Best,

Luciano



On Thu, May 21, 2015 at 11:53 AM, Rich Shepard <rshep...@appl-ecosys.com> wrote:
>   I use emacs for writing code (among other editing tasks) and have the line
> length set to 78 characters. That's the recommended maximum line length in
> PEP 8. However, when I have a lot of text for an import command, or within a
> single-quoted string, python complains about unexpected endings if I break
> the string with a newline prior to column 78.
>
>   Should I just ignore that PEP 8 suggestion or is there a line continuation
> character (such as \) that can be applied?
>
> Rich
> _______________________________________________
> Portland mailing list
> Portland@python.org
> https://mail.python.org/mailman/listinfo/portland



-- 
Luciano Ramalho
|  Author of Fluent Python (O'Reilly, 2015)
|     http://shop.oreilly.com/product/0636920032519.do
|  Professor em: http://python.pro.br
|  Twitter: @ramalhoorg
_______________________________________________
Portland mailing list
Portland@python.org
https://mail.python.org/mailman/listinfo/portland

Reply via email to