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

Discussion in 'Python' started by Zeynel, Nov 13, 2010.

  1. Zeynel

    Zeynel Guest

    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?
    Zeynel, Nov 13, 2010
    #1
    1. Advertising

  2. Zeynel

    MRAB Guest

    On 13/11/2010 18:53, 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 triple-quoted strings:

    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,))
    MRAB, Nov 13, 2010
    #2
    1. Advertising

  3. Zeynel

    Tim Chase Guest

    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
    Tim Chase, Nov 13, 2010
    #3
  4. Zeynel

    Zeynel Guest

    Tim Chase and MRAB: Thanks!!

    On Nov 13, 2:14 pm, Tim Chase <> wrote:
    > 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
    Zeynel, Nov 13, 2010
    #4
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Replies:
    12
    Views:
    945
  2. bolega
    Replies:
    4
    Views:
    518
    Steven D'Aprano
    Jan 14, 2011
  3. Anonieko Ramos

    What's wrong with rpc-literal? Why use doc-literal?

    Anonieko Ramos, Sep 27, 2004, in forum: ASP .Net Web Services
    Replies:
    0
    Views:
    366
    Anonieko Ramos
    Sep 27, 2004
  4. Ciba LO
    Replies:
    4
    Views:
    176
    Ciba LO
    Jul 8, 2006
  5. Cah Sableng
    Replies:
    0
    Views:
    231
    Cah Sableng
    Apr 23, 2007
Loading...

Share This Page