Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #7560 > unrolled thread

split long string in two code lines

Started byTracubik <affdfsdfdsfsd@b.com>
First post2011-06-13 23:31 +0200
Last post2011-06-14 09:11 +1000
Articles 9 — 7 participants

Back to article view | Back to comp.lang.python


Contents

  split long string in two code lines Tracubik <affdfsdfdsfsd@b.com> - 2011-06-13 23:31 +0200
    Re: split long string in two code lines darnold <darnold992000@yahoo.com> - 2011-06-13 14:51 -0700
      Re: split long string in two code lines Terry Reedy <tjreedy@udel.edu> - 2011-06-13 23:55 -0400
    Re: split long string in two code lines Tycho Andersen <tycho@tycho.ws> - 2011-06-13 16:55 -0500
    Re: split long string in two code lines Redcat <redcat@catfolks.net> - 2011-06-13 22:21 +0000
    Re: split long string in two code lines Tim Chase <python.list@tim.thechases.com> - 2011-06-13 17:33 -0500
    Re: split long string in two code lines Chris Angelico <rosuav@gmail.com> - 2011-06-14 08:38 +1000
    Re: split long string in two code lines Tim Chase <python.list@tim.thechases.com> - 2011-06-13 18:03 -0500
    Re: split long string in two code lines Chris Angelico <rosuav@gmail.com> - 2011-06-14 09:11 +1000

#7560 — split long string in two code lines

FromTracubik <affdfsdfdsfsd@b.com>
Date2011-06-13 23:31 +0200
Subjectsplit long string in two code lines
Message-ID<4df681ae$0$2694$4fafbaef@reader1.news.tin.it>
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

[toc] | [next] | [standalone]


#7562

Fromdarnold <darnold992000@yahoo.com>
Date2011-06-13 14:51 -0700
Message-ID<e92570fa-238c-40c4-ad96-3b9b562331ae@w10g2000yqh.googlegroups.com>
In reply to#7560
print "this" \
    " is" \
    " a" \
    " test" \

>>> ================================ RESTART ================================
>>>
this is a test

[toc] | [prev] | [next] | [standalone]


#7584

FromTerry Reedy <tjreedy@udel.edu>
Date2011-06-13 23:55 -0400
Message-ID<mailman.208.1308023746.11593.python-list@python.org>
In reply to#7562
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

[toc] | [prev] | [next] | [standalone]


#7563

FromTycho Andersen <tycho@tycho.ws>
Date2011-06-13 16:55 -0500
Message-ID<mailman.198.1308002118.11593.python-list@python.org>
In reply to#7560
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

[toc] | [prev] | [next] | [standalone]


#7564

FromRedcat <redcat@catfolks.net>
Date2011-06-13 22:21 +0000
Message-ID<95ngqjFvocU6@mid.individual.net>
In reply to#7560
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"

[toc] | [prev] | [next] | [standalone]


#7565

FromTim Chase <python.list@tim.thechases.com>
Date2011-06-13 17:33 -0500
Message-ID<mailman.199.1308004430.11593.python-list@python.org>
In reply to#7560
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


[toc] | [prev] | [next] | [standalone]


#7567

FromChris Angelico <rosuav@gmail.com>
Date2011-06-14 08:38 +1000
Message-ID<mailman.200.1308004689.11593.python-list@python.org>
In reply to#7560
On Tue, Jun 14, 2011 at 8:33 AM, Tim Chase
<python.list@tim.thechases.com> 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

[toc] | [prev] | [next] | [standalone]


#7569

FromTim Chase <python.list@tim.thechases.com>
Date2011-06-13 18:03 -0500
Message-ID<mailman.202.1308006192.11593.python-list@python.org>
In reply to#7560
On 06/13/2011 05:38 PM, Chris Angelico wrote:
> On Tue, Jun 14, 2011 at 8:33 AM, Tim Chase
> <python.list@tim.thechases.com>  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





[toc] | [prev] | [next] | [standalone]


#7571

FromChris Angelico <rosuav@gmail.com>
Date2011-06-14 09:11 +1000
Message-ID<mailman.203.1308006670.11593.python-list@python.org>
In reply to#7560
On Tue, Jun 14, 2011 at 9:03 AM, Tim Chase
<python.list@tim.thechases.com> 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

[toc] | [prev] | [standalone]


Back to top | Article view | comp.lang.python


csiph-web