Multi-Line Strings: A Modest Proposal

John Wong gokoproject at gmail.com
Sat Aug 13 21:53:20 EDT 2016


On Sat, Aug 13, 2016 at 8:38 PM, Lawrence D’Oliveiro <lawrencedo99 at gmail.com
> wrote:

> Python allows a single string literal to cross multiple lines, provided it
> begins and ends with three quote characters, e.g.
>
>     s = """this string continues
>     on the next line."""
>
> There is a drawback with this: any whitespace at the start of the
> continuation line is included as part of the string:
>
>     >>> print(s)
>     this string continues
>         on the next line.
>
>
I think you meant both trailing characters and starting characters.

The way I solve it, and I still find that extremely ugly, is

s = ("this string continues " +
       "substring continues")

The blackslash is a method I used to do but I start regretting. I have ran
into issue with blackslash before, can't remember but, but most likely with
escaping.

It's ugly when I am in the middle of a deep indented function call. Imagine
I am writing an exception

def foo(...)
      ....

              raise SomeErrorKindofLong("This is an exception and I need to
write something long {v1} so I need to wrap around it {v2}".format(v1=v1,
v2=v2))

You get the point... by wrapping, I ended up breaking this into multiple
lines. In some cases I would build the string before calling the exception.

That seems a little tangent, but relevant.

The problem with this proposal is that could break existing application.
It's impossible to switch to this convention.

John



More information about the Python-list mailing list