Building a multiline string

Discussion in 'Python' started by lallous, Feb 4, 2010.

  1. lallous

    lallous Guest

    Hello

    Maybe that's already documented, but it seems the parser accepts to
    build a long string w/o really using the first method:

    # Method1
    x = "line1" + \ # cannot use comments!
    "line2"+ \
    "line3"

    and instead using a list with one element like this:

    # Method2
    x = [
    "line1" # can use comments
    "line2"
    "line3"
    ][0]

    Or:
    # Method3
    x = (
    "line1" # can use comments
    "line2"
    "line3"
    )

    (Not that I don't want new lines in the strings)

    Now should I be using method 2 or 3 in production code?

    --
    Elias
    lallous, Feb 4, 2010
    #1
    1. Advertising

  2. Just for the record: Neither of the below methods actually produce a
    multiline string. They only spread a string containing one line over
    multiple lines of source code.

    lallous wrote:
    > Maybe that's already documented, but it seems the parser accepts to
    > build a long string w/o really using the first method:
    >
    > # Method1
    > x = "line1" + \ # cannot use comments!
    > "line2"+ \
    > "line3"


    Well, obviously you can't use comments like that there. The point of the
    backslash is that it continues the current logical line over the
    _immediately_ _following_ newline. If anything follows, that obviously
    doesn't work.

    > and instead using a list with one element like this:
    >
    > # Method2
    > x = [
    > "line1" # can use comments
    > "line2"
    > "line3"
    > ][0]


    This basically makes use of the fact that "this" "is" "one" "string" and not
    four strings.

    > # Method3
    > x = (
    > "line1" # can use comments
    > "line2"
    > "line3"
    > )


    This uses the same, only that this time it uses brackets which cause an
    expression to extend to multiple lines.

    > (Not that I don't want new lines in the strings)


    You don't not want or you don't want newlines? Depending on that, you could
    also do this:

    # method 4
    x = "line1"\
    "line2"\
    "line3"

    or maybe

    # method 5
    x = """line1
    line2
    line3
    """


    > Now should I be using method 2 or 3 in production code?


    I'd go for 3 or 4. 2 is basically a hack (you could do the same with a
    dictionary, or a tuple, not only a list). 1 will actually create strings
    and then concatenate them (unless Python is smart enough to optimize that),
    but it allows adding expressions in the middle.

    Uli

    --
    Sator Laser GmbH
    Geschäftsführer: Thorsten Föcking, Amtsgericht Hamburg HR B62 932
    Ulrich Eckhardt, Feb 4, 2010
    #2
    1. Advertising

  3. lallous

    Steve Holden Guest

    lallous wrote:
    > Hello
    >
    > Maybe that's already documented, but it seems the parser accepts to
    > build a long string w/o really using the first method:
    >
    > # Method1
    > x = "line1" + \ # cannot use comments!
    > "line2"+ \
    > "line3"
    >
    > and instead using a list with one element like this:
    >
    > # Method2
    > x = [
    > "line1" # can use comments
    > "line2"
    > "line3"
    > ][0]
    >
    > Or:
    > # Method3
    > x = (
    > "line1" # can use comments
    > "line2"
    > "line3"
    > )
    >
    > (Not that I don't want new lines in the strings)
    >
    > Now should I be using method 2 or 3 in production code?
    >

    I should have thought it was pretty obvious that method 2 creates a list
    and then performs an indexing operation on it. These are completely
    unnecessary operations, which are avoided in method 3 which is a simple
    parenthesised expression.

    So why anyone would want to adopt method 2, which is also mess clear as
    source code, is beyond me.

    regards
    Steve
    --
    Steve Holden +1 571 484 6266 +1 800 494 3119
    PyCon is coming! Atlanta, Feb 2010 http://us.pycon.org/
    Holden Web LLC http://www.holdenweb.com/
    UPCOMING EVENTS: http://holdenweb.eventbrite.com/
    Steve Holden, Feb 4, 2010
    #3
  4. On 02/04/2010 12:34 PM, lallous wrote:

    > Now should I be using method 2 or 3 in production code?


    Another way... depending on what you are using the string for, of
    course. If it's an HTML/XML/SQL/whatever piece of code:

    >>>> from textwrap import dedent
    >>>> sql = dedent("""

    > ... SELECT *
    > ... FROM table
    > ... WHERE foo=bar
    > ... """)
    >>>>
    >>>> print sql

    >
    > SELECT *
    > FROM table
    > WHERE foo=bar
    >



    And if you don't want the starting/ending newlines:

    >>>> sql = dedent("""\

    > ... SELECT *
    > ... FROM table
    > ... WHERE foo=bar\
    > ... """)
    >>>>
    >>>> print sql

    > SELECT *
    > FROM table
    > WHERE foo=bar
    >>>>


    I use this sometimes to keep both python and the embedded code readable
    while preserving indentation.
    Marco Mariani, Feb 4, 2010
    #4
  5. lallous

    lallous Guest

    @Ulrich:

    On Feb 4, 1:09 pm, Ulrich Eckhardt <> wrote:
    > Just for the record: Neither of the below methods actually produce a
    > multiline string. They only spread a string containing one line over
    > multiple lines of source code.
    >


    I meant:
    "Note" -> "Note: I don't want to use new lines"

    I did not want a multi line string


    Thanks guys, method 3 seems to be good enough.
    lallous, Feb 5, 2010
    #5
  6. lallous

    Aahz Guest

    In article <>,
    lallous <> wrote:
    >
    >x = (
    >"line1" # can use comments
    >"line2"
    >"line3"
    >)


    You should indent the second and following lines (I changed the name to
    "xyz" to make clear that the following lines use a regular Python indent
    rather than lining up under the open paren):

    xyz = (
    "line1" # can use comments
    "line2"
    "line3"
    )
    --
    Aahz () <*> http://www.pythoncraft.com/

    import antigravity
    Aahz, Feb 8, 2010
    #6
    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. Midex
    Replies:
    24
    Views:
    866
  2. Danny Shevitz
    Replies:
    3
    Views:
    290
    Danny Shevitz
    Apr 30, 2008
  3. Robert Dailey

    Need cleanup advice for multiline string

    Robert Dailey, Aug 11, 2009, in forum: Python
    Replies:
    62
    Views:
    1,203
    Stefan Behnel
    Aug 24, 2009
  4. dale zhang
    Replies:
    8
    Views:
    393
    Tintin
    Nov 30, 2004
  5. Peng Yu
    Replies:
    2
    Views:
    129
    Tad J McClellan
    Jun 24, 2008
Loading...

Share This Page