From: Lie Ryan on
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"<goldtech(a)worldpost.com> wrote in message
>> news:4e25733e-eafa-477b-a84d-a64d139f7134(a)u34g2000yqu.googlegroups.com...
>> On Apr 27, 7:31 pm, Brendan Abel<007bren...(a)gmail.com> wrote:
>>> On Apr 27, 7:20 pm, goldtech<goldt...(a)worldpost.com> 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; there is no need to split the string up manually or even
scriptically.

d = """
�Twas brillig, and the slithy toves
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.
From: Alf P. Steinbach on
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"<goldtech(a)worldpost.com> wrote in message
>>> news:4e25733e-eafa-477b-a84d-a64d139f7134(a)u34g2000yqu.googlegroups.com...
>>> On Apr 27, 7:31 pm, Brendan Abel<007bren...(a)gmail.com> wrote:
>>>> On Apr 27, 7:20 pm, goldtech<goldt...(a)worldpost.com> 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
From: Lie Ryan on
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.
From: Steven D'Aprano on
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
From: Ben Finney on
Lie Ryan <lie.1296(a)gmail.com> writes:

> Python have triple-quoted string when you want to include large amount
> of text; there is no need to split the string up manually or even
> scriptically.

You can even have multi-line string literals that have correct
indentation in the code, but strip that indentation at runtime::

import textwrap

def foo():
""" Gyre and gimble in the wabe. """
whiffle(textwrap.dedent("""\
Jabberwocky

'Twas brillig, and the slithy toves
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.

—Lewis Carroll
"""))

def whiffle(text):
""" Show the lines that have leading indentation. """
for line in text.split("\n"):
if line.startswith((" ", "\t")):
print(line)

foo()

The multi-line string is indented nicely within the code. When I run
this, I get::

Jabberwocky
—Lewis Carroll

showing that the only lines still indented are those that we *want* to
be indented within the string.

--
\ “If consumers even know there's a DRM, what it is, and how it |
`\ works, we've already failed.” —Peter Lee, Disney corporation, |
_o__) 2005 |
Ben Finney