Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > assigning multi-line strings to variables

Reply
Thread Tools

assigning multi-line strings to variables

 
 
Alf P. Steinbach
Guest
Posts: n/a
 
      04-28-2010
On 28.04.2010 18:54, * Lie Ryan:
> On 04/28/10 15:34, Alf P. Steinbach wrote:
>> On 28.04.2010 07:11, * Sagar K:
>>> Use triple quote:
>>> d = """ this is
>>> a sample text
>>> which does
>>> not mean
>>> anything"""
>>>
>>> "goldtech"<(E-Mail Removed)> wrote in message
>>> news:(E-Mail Removed)...
>>> On Apr 27, 7:31 pm, Brendan Abel<(E-Mail Removed)> wrote:
>>>> On Apr 27, 7:20 pm, goldtech<(E-Mail Removed)> wrote:
>>>>
>>>>> Hi,
>>>>
>>>>> This is undoubtedly a newbie question. How doI assign variables
>>>>> multiline strings? If I try this i get what's cited below. Thanks.
>>>>
>>>>>>>> d="ddddd
>>>>> ddddd"
>>>>>>>> d
>>>>
>>>>> Traceback (most recent call last):
>>>>> File "<interactive input>", line 1, in<module>
>>>>> NameError: name 'd' is not defined
>>>>
>>>> d = "ddddddddd"\
>>>> "ddddd"
>>>>
>>>> or
>>>>
>>>> d = "dddddddddd\
>>>> dddddd"
>>>>
>>>> You don't need the trailing slash in the first example if you are
>>>> writing this in a script, python assumes it.
>>>
>>> Thanks but what if the string is 500 lines. Seems it would be hard to
>>> put a "\" manually at the end of every line. How could i do that?

>>
>> That depends. You can put the string in a separate text file and read
>> the file, or you can have it as a literal. For the latter your editor
>> should provide you with the tools to format the string any which way you
>> want, and if not, then just a write a Python script to format it for you.
>>
>> Consider this little example[1]:

> <snip>
>
> Python have triple-quoted string when you want to include large amount
> of text;


Yes, that's been mentioned umpteen times in this thread, including the *very
first* quoted sentence above.

It's IMHO sort of needless to repeat that after quoting it, and providing yet
another example right after quoting an example.

Probably you didn't notice?


> there is no need to split the string up manually or even
> scriptically.


Consider that the concatenation language feature probably is there because it's
useful (e.g. it preserves indentation and allows per line comments).


> d = """
> ’Twas brillig, and the slithy toves


Here you have introduced an unintentional linebreak, oops.


> Did gyre and gimble in the wabe;
> All mimsy were the borogoves,
> And the mome raths outgrabe.
>
> “Beware the Jabberwock, my son!
> The jaws that bite, the claws that catch!
> Beware the Jubjub bird, and shun
> The frumious Bandersnatch!”
>
> He took his vorpal sword in hand:
> Long time the manxome foe he sought—
> So rested he by the Tumtum tree,
> And stood awhile in thought.
>
> And as in uffish thought he stood,
> The Jabberwock, with eyes of flame,
> Came whiffling through the tulgey wood,
> And burbled as it came!
>
> One, two! One, two! and through and through
> The vorpal blade went snicker-snack!
> He left it dead, and with its head
> He went galumphing back.
>
> “And hast thou slain the Jabberwock?
> Come to my arms, my beamish boy!
> O frabjous day! Callooh! Callay!”
> He chortled in his joy.
>
> ’Twas brillig, and the slithy toves
> Did gyre and gimble in the wabe;
> All mimsy were the borogoves,
> And the mome raths outgrabe.
> """
>
> I copied that in less then 10 seconds.


Doesn't matter how fast it is when it's not correct (or, from another point of
view, if it doesn't need to be done correctly then it can be arbitrarily fast).

Of course you can fix it, but since you posted it with errors I think you were
not aware.

In the end there are drawbacks to any way of doing it, so it's to a large degree
a matter of personal preference, as I see it. I just mentioned two additional
ways not yet discussed in the thread. Exemplifying one of them.


Cheers,

- Alf
 
Reply With Quote
 
 
 
 
Lie Ryan
Guest
Posts: n/a
 
      04-28-2010
On 04/29/10 04:16, Alf P. Steinbach wrote:
> On 28.04.2010 18:54, * Lie Ryan:
>> On 04/28/10 15:34, Alf P. Steinbach wrote:

>
> Yes, that's been mentioned umpteen times in this thread, including the
> *very first* quoted sentence above.
>
> It's IMHO sort of needless to repeat that after quoting it, and
> providing yet another example right after quoting an example.
>
> Probably you didn't notice?


I do, my complaints is that you're making it needlessly complex.

>> there is no need to split the string up manually or even
>> scriptically.

>
> Consider that the concatenation language feature probably is there
> because it's useful (e.g. it preserves indentation and allows per line
> comments).


No, the implicit concatenation is there because Python didn't always
have triple quoted string. Nowadays it's an artifact and triple quoted
string is much preferred. Long dump of text in source code is usually
(or should be) globals constant anyway and you have no problem about
indentation in globals constant. The only ubiquitous place where you
need to put long stream of triple-quoted in non-global is as docstring;
and docstring already handles the problem with indentation.

>> I copied that in less then 10 seconds.

>
> Doesn't matter how fast it is when it's not correct (or, from another
> point of view, if it doesn't need to be done correctly then it can be
> arbitrarily fast).


It's the nature of the text, who cares about extra line breaks or two.
If this string has been for regex, I'd probably care.

> Of course you can fix it, but since you posted it with errors I think
> you were not aware.


I used triple-quote a lot, and I'm very aware of that.

> In the end there are drawbacks to any way of doing it, so it's to a
> large degree a matter of personal preference, as I see it. I just
> mentioned two additional ways not yet discussed in the thread.
> Exemplifying one of them.


When you have long stream of text and you paste it in source code, it's
usually for one-off scripts that you're too lazy to create a proper file
to read from. You never want to have to do some tedious preprocessing
before you can insert it to the source.

There's never a good reason to insert very long streams of text using
implicit concatenation; not since docstring is here.
 
Reply With Quote
 
 
 
 
Steven D'Aprano
Guest
Posts: n/a
 
      04-28-2010
On Thu, 29 Apr 2010 06:17:42 +1000, Lie Ryan wrote:

>> Consider that the concatenation language feature probably is there
>> because it's useful (e.g. it preserves indentation and allows per line
>> comments).

>
> No, the implicit concatenation is there because Python didn't always
> have triple quoted string.


Do you have a source for that?

Both triple-quoted strings and implicit concatenation go back to at least
Python 1.4:

http://docs.python.org/release/1.4/tut/node71.html
http://docs.python.org/release/1.4/tut/node70.html



--
Steven
 
Reply With Quote
 
MRAB
Guest
Posts: n/a
 
      04-29-2010
Steven D'Aprano wrote:
> On Thu, 29 Apr 2010 06:17:42 +1000, Lie Ryan wrote:
>
>>> Consider that the concatenation language feature probably is there
>>> because it's useful (e.g. it preserves indentation and allows per line
>>> comments).

>> No, the implicit concatenation is there because Python didn't always
>> have triple quoted string.

>
> Do you have a source for that?
>
> Both triple-quoted strings and implicit concatenation go back to at least
> Python 1.4:
>
> http://docs.python.org/release/1.4/tut/node71.html
> http://docs.python.org/release/1.4/tut/node70.html
>

The page here:

http://svn.python.org/projects/pytho...k/Misc/HISTORY

says release 1.0.2 (4 May 1994).
 
Reply With Quote
 
Steven D'Aprano
Guest
Posts: n/a
 
      04-29-2010
On Thu, 29 Apr 2010 02:16:46 +0100, MRAB wrote:

> Steven D'Aprano wrote:
>> On Thu, 29 Apr 2010 06:17:42 +1000, Lie Ryan wrote:
>>
>>>> Consider that the concatenation language feature probably is there
>>>> because it's useful (e.g. it preserves indentation and allows per
>>>> line comments).
>>> No, the implicit concatenation is there because Python didn't always
>>> have triple quoted string.

>>
>> Do you have a source for that?
>>
>> Both triple-quoted strings and implicit concatenation go back to at
>> least Python 1.4:
>>
>> http://docs.python.org/release/1.4/tut/node71.html
>> http://docs.python.org/release/1.4/tut/node70.html
>>

> The page here:
>
> http://svn.python.org/projects/pytho...k/Misc/HISTORY
>
> says release 1.0.2 (4 May 1994).


Yes, it says:

* String literals follow Standard C rules: they may be continued
on the next line using a backslash; adjacent literals are
concatenated at compile time.

* A new kind of string literals, surrounded by triple quotes
(""" or '''), can be continued on the next line without a
backslash.


These are adjacent entries in the same release. That's pretty good
evidence that both implicit concatenation and triple quotes were
introduced at the same time.



--
Steven
 
Reply With Quote
 
Gregory Ewing
Guest
Posts: n/a
 
      04-29-2010
Lie Ryan wrote:
> No, the implicit concatenation is there because Python didn't always
> have triple quoted string. Nowadays it's an artifact and triple quoted
> string is much preferred.


I don't agree. I often use implicit concatenation when I'm
writing a format string that won't fit on one source line,
because it allows me to fit it into the surrounding indentation
structure without introducing unwanted spaces into the string.

Both tecnhiques have their places.

--
Greg
 
Reply With Quote
 
goldtech
Guest
Posts: n/a
 
      04-29-2010
Thank you to posters for help to my question. Seems I had trouble with
triple quotes strings in the PythonWin shell. But using the Idle shell
things work as expected. But this is probably another issue...any way,
w/Idle's shell I got the "action" regarding multiline strings I
expected.
 
Reply With Quote
 
Lie Ryan
Guest
Posts: n/a
 
      04-29-2010
On 04/29/10 20:40, Gregory Ewing wrote:
> Lie Ryan wrote:
>> No, the implicit concatenation is there because Python didn't always
>> have triple quoted string. Nowadays it's an artifact and triple quoted
>> string is much preferred.

>
> I don't agree. I often use implicit concatenation when I'm
> writing a format string that won't fit on one source line,
> because it allows me to fit it into the surrounding indentation
> structure without introducing unwanted spaces into the string.
>
> Both tecnhiques have their places.
>


That statement should be quantified with "for large chunks of text".
Format string is typically 2-3 lines at most, not enough to qualify as
large chunk.
 
Reply With Quote
 
Lie Ryan
Guest
Posts: n/a
 
      04-29-2010
On 04/29/10 16:34, Steven D'Aprano wrote:
> On Thu, 29 Apr 2010 02:16:46 +0100, MRAB wrote:
>
>> Steven D'Aprano wrote:
>>> On Thu, 29 Apr 2010 06:17:42 +1000, Lie Ryan wrote:
>>>
>>>>> Consider that the concatenation language feature probably is there
>>>>> because it's useful (e.g. it preserves indentation and allows per
>>>>> line comments).
>>>> No, the implicit concatenation is there because Python didn't always
>>>> have triple quoted string.
>>>
>>> Do you have a source for that?
>>>
>>> Both triple-quoted strings and implicit concatenation go back to at
>>> least Python 1.4:
>>>
>>> http://docs.python.org/release/1.4/tut/node71.html
>>> http://docs.python.org/release/1.4/tut/node70.html
>>>

>> The page here:
>>
>> http://svn.python.org/projects/pytho...k/Misc/HISTORY
>>
>> says release 1.0.2 (4 May 1994).

>
> Yes, it says:
>
> * String literals follow Standard C rules: they may be continued
> on the next line using a backslash; adjacent literals are
> concatenated at compile time.
>
> * A new kind of string literals, surrounded by triple quotes
> (""" or '''), can be continued on the next line without a
> backslash.
>
>
> These are adjacent entries in the same release. That's pretty good
> evidence that both implicit concatenation and triple quotes were
> introduced at the same time.


Yes, apparently my statement that implicit concatenation is an artifact
is erroneous but it doesn't make the important points less true, that
implicit concatenation is not suitable for integrating large chunk of
text into source code.

And if you do care about the two extra newlines, you can add two
backslashes:

s = """\
....
insert large chunks
....\
"""

which is a constant-time preformatting compared to prepending and
appending every line with quotes, which is O(n) (and we're talking about
the oh-so-expensive programmer's time here).

Ben Finney also showed the trick to let a large chunk to appear indented
and stripping the indenting at runtime in another thread (which is
actually rarely needed since, since as I previously said, huge chunk of
text is usually global-level variable; though the trick does come handy
sometimes).
 
Reply With Quote
 
Carl Banks
Guest
Posts: n/a
 
      04-29-2010
On Apr 28, 11:16*am, "Alf P. Steinbach" <(E-Mail Removed)> wrote:
> On 28.04.2010 18:54, * Lie Ryan:


> > Python have triple-quoted string when you want to include large amount
> > of text;

>
> Yes, that's been mentioned umpteen times in this thread, including the *very
> first* quoted sentence above.
>
> It's IMHO sort of needless to repeat that after quoting it, and providing yet
> another example right after quoting an example.
>
> Probably you didn't notice?



I think he repeated it just to let people know that they can get what
they want without following your adsurd advice.


Carl Banks
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Put variables into member variables or function variables? tjumail@gmail.com C++ 9 03-23-2008 04:03 PM
Assigning methods to objects, and assigning onreadystatechange to an XMLHttpRequest -- an inconsistency? weston Javascript 1 09-22-2006 09:33 AM
Strings, Strings and Damned Strings Ben C Programming 14 06-24-2006 05:09 AM
How to construct new variables in a script from other variables and strings. gerry.brennan@gmail.com Perl Misc 9 05-12-2006 06:00 PM
Need Help: AccessDataSource and Assigning Variables for Data (ASP 2.0) Michael ASP .Net 1 01-30-2006 02:57 AM



Advertisments