assigning multi-line strings to variables

Alf P. Steinbach alfps at
Wed Apr 28 14:16:50 EDT 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"<goldtech at>   wrote in message
>>> news:4e25733e-eafa-477b-a84d-a64d139f7134 at
>>> On Apr 27, 7:31 pm, Brendan Abel<007bren... at>   wrote:
>>>> On Apr 27, 7:20 pm, goldtech<goldt... at>   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.


- Alf

More information about the Python-list mailing list