Break lines?

Discussion in 'Python' started by saneman, Feb 26, 2008.

  1. saneman

    saneman Guest

    I have made this string:


    TITLE = 'Efficiency of set operations: sort model,
    (cphstl::set::insert(p,e)^n cphstl::set::insert(e)), integer'

    But I am not allowed to break the line like that:

    IndentationError: unexpected indent

    How do I break a line?
    saneman, Feb 26, 2008
    #1
    1. Advertising

  2. saneman

    Tim Chase Guest

    > I have made this string:
    >
    > TITLE = 'Efficiency of set operations: sort model,
    > (cphstl::set::insert(p,e)^n cphstl::set::insert(e)), integer'
    >
    > But I am not allowed to break the line like that:
    >
    > IndentationError: unexpected indent
    >
    > How do I break a line?


    Depends on what you want. You can embed running strings with
    newlines using triple-quotes (either single- or double-quotes):

    TITLE = """Efficiency...
    (cphstl:..."""


    Or you can use string concatenation using line-continuations:

    TITLE = "Efficiency..." \
    "(cphstl:..."

    or using parens

    TITLE = ("Efficiency..."
    "(cphstl:...")



    I like the clean'ness of the first version, but sometimes get
    irked by it including my leading whitespace (there are some
    workarounds, but all involve more than trivial effort). I tend
    to use the 2nd in the case you describe, but usually using the
    3rd version in all other cases where it's as a parameter to a
    function call or some other bracketed/braced construct.

    -tkc
    Tim Chase, Feb 26, 2008
    #2
    1. Advertising

  3. saneman

    saneman Guest

    Tim Chase wrote:
    >> I have made this string:
    >>
    >> TITLE = 'Efficiency of set operations: sort model,
    >> (cphstl::set::insert(p,e)^n cphstl::set::insert(e)), integer'
    >>
    >> But I am not allowed to break the line like that:
    >>
    >> IndentationError: unexpected indent
    >>
    >> How do I break a line?

    >
    > Depends on what you want. You can embed running strings with newlines
    > using triple-quotes (either single- or double-quotes):
    >
    > TITLE = """Efficiency...
    > (cphstl:..."""
    >
    >
    > Or you can use string concatenation using line-continuations:
    >
    > TITLE = "Efficiency..." \
    > "(cphstl:..."
    >
    > or using parens
    >
    > TITLE = ("Efficiency..."
    > "(cphstl:...")
    >
    >
    >
    > I like the clean'ness of the first version, but sometimes get irked by
    > it including my leading whitespace (there are some workarounds, but all
    > involve more than trivial effort). I tend to use the 2nd in the case
    > you describe, but usually using the 3rd version in all other cases where
    > it's as a parameter to a function call or some other bracketed/braced
    > construct.
    >
    > -tkc
    >
    >


    Ok thanks! Btw why double quotes " instead of single ' ?
    saneman, Feb 26, 2008
    #3
  4. saneman

    Tim Chase Guest

    > Ok thanks! Btw why double quotes " instead of single ' ?

    Either one will do...there's not much difference. I try to use
    double-quotes most of the time, just so when I include an
    apostrophe in-line (which I do more often than I include a
    double-quote in-line), I don't have to think.

    string1a = "John's dog"
    string1b = 'John\'s dog'
    string2a = "She said \"hello\""
    string2b = 'She said "hello"'
    string3a = 'She said "John\'s nice" in a letter'
    string3b = "She said \"John's nice\" in a letter'
    string3c = """She said "John's nice" in a letter"""
    string3d = '''She said "John's nice" in a letter'''

    My usual aim is for clarity, so I tend to go with the versions
    that have the fewest backslashes in them.

    -tkc
    Tim Chase, Feb 26, 2008
    #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. DaVinci

    how to make it break lines

    DaVinci, May 20, 2006, in forum: HTML
    Replies:
    7
    Views:
    554
    Jukka K. Korpela
    May 21, 2006
  2. Wai Yip Tung
    Replies:
    5
    Views:
    1,110
    Andrew Dalke
    Sep 4, 2004
  3. Replies:
    12
    Views:
    957
  4. Zeynel
    Replies:
    3
    Views:
    357
    Zeynel
    Nov 13, 2010
  5. Ciba LO
    Replies:
    4
    Views:
    182
    Ciba LO
    Jul 8, 2006
Loading...

Share This Page