FAQ
Hi all,

newbie question here

how can i write code like this:

1 def foo():
2 for index in ...
3 for plsdoit in ...
4 print "this is a very long string that i'm going to write 5
here, it'll be for sure longer than 80 columns"


the only way i've found is to use the "/", but than i've to write
something like this:

1 def foo():
2 for index in ...
3 for plsdoit in ...
4 print "this is a very long string that i'm going to/
5 write here, it'll be for sure longer than 80 columns"

what i don't really like is that line 5 is not indented. if i indent it,
the spaces will be counted as spaces of the string.

Is there a better way to split the string?
thanks for your kind help
Nico

Search Discussions

  • Darnold at Jun 13, 2011 at 9:51 pm
    print "this" \
    " is" \
    " a" \
    " test" \
    ================================ RESTART ================================
    this is a test
  • Terry Reedy at Jun 14, 2011 at 3:55 am

    On 6/13/2011 5:51 PM, darnold wrote:
    print "this" \
    " is" \
    " a" \
    " test" \
    this is a test
    print('this'
    ' is'
    ' a'
    ' test')
    this is a test

    Python ignores \n within parentheses, brackets, and braces, so no
    fragile trailing backslash needed. ('Fragile', because invisible space
    or tab after '\' is an error.)

    --
    Terry Jan Reedy
  • Tycho Andersen at Jun 13, 2011 at 9:55 pm

    On Mon, Jun 13, 2011 at 11:31:29PM +0200, Tracubik wrote:
    Hi all,

    newbie question here

    how can i write code like this:

    1 def foo():
    2 for index in ...
    3 for plsdoit in ...
    4 print "this is a very long string that i'm going to
    write 5 here, it'll be for sure longer than 80 columns"


    the only way i've found is to use the "/", but than i've to write
    something like this:
    Perhaps you mean '\'?
    1 def foo():
    2 for index in ...
    3 for plsdoit in ...
    4 print "this is a very long string that i'm going to/
    5 write here, it'll be for sure longer than 80 columns"

    what i don't really like is that line 5 is not indented. if i indent
    it, the spaces will be counted as spaces of the string.

    Is there a better way to split the string?
    There is! Python (as C) concatenates string literals with nothing in
    between them.

    Python 2.6.2 (r262:71600, Jun 8 2009, 11:11:42)
    [GCC 4.3.2] on linux2
    Type "help", "copyright", "credits" or "license" for more information.
    def foo():
    ... print "this is not such a huge line " \
    ... "but it's still pretty long"
    ...
    foo()
    this is not such a huge line but it's still pretty long

    \t
  • Tim Chase at Jun 13, 2011 at 10:33 pm

    On 06/13/2011 04:55 PM, Tycho Andersen wrote:
    On Mon, Jun 13, 2011 at 11:31:29PM +0200, Tracubik wrote:
    4 print "this is a very long string that i'm going to
    write 5 here, it'll be for sure longer than 80 columns"

    Is there a better way to split the string?
    There is! Python (as C) concatenates string literals with nothing in
    between them.
    def foo():
    ... print "this is not such a huge line " \
    ... "but it's still pretty long"
    ...
    foo()
    this is not such a huge line but it's still pretty long
    Python also treats consecutive strings as a single string, so you
    can do things like

    print ("this is not "
    "such a huge line "
    "even though it has "
    "lots of text in it."
    )

    I tend to put the closing paren on its own line just to minimize
    noise in my VCS diffs when the text changes. Truth be told, I
    often put the opening paren separate from the text:

    print (
    "this is not "
    "such a huge line "
    "even though it has "
    "lots of text in it."
    )

    for the same reason, even though I know some folks on the list
    occasionally grouse about dangling-parens.

    -tkc
  • Chris Angelico at Jun 13, 2011 at 10:38 pm

    On Tue, Jun 14, 2011 at 8:33 AM, Tim Chase wrote:
    ?print ("this is not "
    ? ?"such a huge line "
    ? ?"even though it has "
    ? ?"lots of text in it."
    ? ?)

    ?print (
    ? ?"this is not "
    ? ?"such a huge line "
    ? ?"even though it has "
    ? ?"lots of text in it."
    ? ?)
    I'm not seeing the difference between these two. Pointer, please? *puzzled*

    Related point: Do you indent the ) to the same level as the opening
    quote on each line, or do you backdent it to the level of the
    statement? And, does it (either way) feel like you're writing braces
    in C?

    ChrisA
  • Tim Chase at Jun 13, 2011 at 11:03 pm

    On 06/13/2011 05:38 PM, Chris Angelico wrote:
    On Tue, Jun 14, 2011 at 8:33 AM, Tim Chase
    wrote:
    print ("this is not "
    "such a huge line "
    "even though it has "
    "lots of text in it."
    )

    print (
    "this is not "
    "such a huge line "
    "even though it has "
    "lots of text in it."
    )
    I'm not seeing the difference between these two. Pointer, please? *puzzled*
    Sorry...tried to make that clear in the surrounding text. The
    first one has the open-paren on the same line as the starting
    line of content-text; the second one just has "print (" on the
    first line without the text (which is on the following line).
    Related point: Do you indent the ) to the same level as the opening
    quote on each line, or do you backdent it to the level of the
    statement? And, does it (either way) feel like you're writing braces
    in C?
    My personal tastes run to your first form (the close-paren at the
    same indent level as the text) which makes it easy to use Vim's
    indent-based folding the way I mostly like. I do (well,
    "did"...I try to shirk C/C++ these days because I just feel so
    unproductive compared to coding in Python) the same in my own C
    code for the same reason. But if employer-standards dictate
    otherwise, when in Rome, render onto Caesar (to throw two
    aphorisms in the blender :)

    -tkc
  • Chris Angelico at Jun 13, 2011 at 11:11 pm

    On Tue, Jun 14, 2011 at 9:03 AM, Tim Chase wrote:
    On 06/13/2011 05:38 PM, Chris Angelico wrote:
    I'm not seeing the difference between these two. Pointer, please?
    *puzzled*
    Sorry...tried to make that clear in the surrounding text. ?The first one has
    the open-paren on the same line as the starting line of content-text; the
    second one just has "print (" on the first line without the text (which is
    on the following line).
    Oh! Duh. I am blind as a bat today... for some reason I was staring at
    the close parens.
    Related point: Do you indent the ) to the same level as the opening
    quote on each line, or do you backdent it to the level of the
    statement? And, does it (either way) feel like you're writing braces
    in C?
    My personal tastes run to your first form (the close-paren at the same
    indent level as the text) which makes it easy to use Vim's indent-based
    folding the way I mostly like. ?I do (well, "did"...I try to shirk C/C++
    these days because I just feel so unproductive compared to coding in Python)
    the same in my own C code for the same reason. ?But if employer-standards
    dictate otherwise, when in Rome, render onto Caesar (to throw two aphorisms
    in the blender :)
    Aye. Helps to have enough seniority to be able to dictate indent
    styles, but otherwise, you just accept it and do it. I was asking
    about personal preference there.

    Folding's a Good Thing, and even if you don't have an actual editor
    facility that works that way (SciTE doesn't use indentation for that
    IMHO), it's visually logical to go as far as the backdent. But on the
    other hand, the rest of Python doesn't work that way - the end of an
    if/for/while is the end of the indent, it doesn't include the
    backdented line. Choices, choices!

    ChrisA
  • Redcat at Jun 13, 2011 at 10:21 pm

    On Mon, 13 Jun 2011 23:31:29 +0200, Tracubik wrote:

    1 def foo():
    2 for index in ...
    3 for plsdoit in ...
    4 print "this is a very long string that i'm going to/ 5
    write here, it'll be for sure longer than 80 columns"
    If you're going to use the \ anyway, how about:
    1 def foo():
    2 for index in ...
    3 for plsdoit in ...
    4 print "this is a very long string that i'm going to "
    5 + "write here, it'll be for sure longer than 80
    columns"

Related Discussions

Discussion Navigation
viewthread | post
Discussion Overview
grouppython-list @
categoriespython
postedJun 13, '11 at 9:31p
activeJun 14, '11 at 3:55a
posts9
users7
websitepython.org

People

Translate

site design / logo © 2022 Grokbase