Is it possible to break a string literal into multiple lines?

Tim Chase python.list at tim.thechases.com
Sat Nov 13 14:14:27 EST 2010


On 11/13/2010 12:53 PM, Zeynel wrote:
> I have string formatting line in Google App Engine webframe webapp:
>
> self.response.out.write("<b>%s</b>:<br />  mWEIGHT: %s<br />
> mDATE0_integer: %s<br />  mCOUNT: %s<br />" % (result.mUNIQUE,
> result.mWEIGHT, mDATE0_integer, result.mCOUNT,))
>
> I would like to be able to write it as
>
> self.response.out.write("<b>%s</b>:<br />
>                                    mWEIGHT: %s<br />
>                                    mDATE0_integer: %s<br />
>                                   mCOUNT: %s<br />"
>                                                              %
> (result.mUNIQUE,
>
> result.mWEIGHT,
>
> mDATE0_integer,
>
> result.mCOUNT,))
>
> But neither \ or enclosing the string in parens let me break the
> string literal enclosed in "" Is this possible?

Use python's triple-quoted strings:

   self.response.out.write("""<b>%s</b>:<br />
        mWEIGHT: %s ...
        ... <br />""")

Or alternatively, you can do something like

   self.response.out.write(
     "<b>%s</b>:br />"
     "mWEIGHT: %s ..."
     ...
     "...<br />"
     )

(that excludes newlines and leading whitespace in the string that 
gets written, but you can modify the string contents to include 
them if you need/want)

-tkc








More information about the Python-list mailing list