assigning multi-line strings to variables

Alf P. Steinbach alfps at start.no
Wed Apr 28 01:34:11 EDT 2010


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 worldpost.com>  wrote in message
> news:4e25733e-eafa-477b-a84d-a64d139f7134 at u34g2000yqu.googlegroups.com...
> On Apr 27, 7:31 pm, Brendan Abel<007bren... at gmail.com>  wrote:
>> On Apr 27, 7:20 pm, goldtech<goldt... at 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]:


<code file="jabberwocky.py">
"The Jabberwocky poem, by Lewis Carrol"

text = (
     "'Twas brillig, and the slithy toves\n"
     "Did gyre and gimble in the wabe;\n"
     "  All mimsy were the borogoves,\n"
     "    And the mome raths outgrabe.\n"
     "\n"
     "\"Beware the jabberwock, my son!\n"
     "  The jaws that bite, the claws that catch!\n"
     "Beware the jubjub bird, and shun\n"
     "  The frumious bandersnatch!\"\n"
     "\n"
     "He took his vorpal sword in hand:\n"
     "  Long time the manxome foe he sought--\n"
     "So rested he by the tumtum tree,\n"
     "  And stood awhile in thought.\n"
     "\n"
     "And as in uffish thought he stood,\n"
     "  The jabberwock, with eyes of flame,\n"
     "Came whiffling through the tulgey wood,\n"
     "  And burbled as it came!\n"
     "\n"
     "One, two! one, two! and through and through\n"
     "  The vorpal blade went snicker-snack!\n"
     "He left it dead, and with its head\n"
     "  He went galumphing back.\n"
     "\n"
     "\"And hast thou slain the jabberwock?\n"
     "  Come to my arms, my beamish boy!\n"
     "O frabjous day! callooh! callay!\"\n"
     "  He chortled in his joy.\n"
     "\n"
     "'Twas brillig, and the slithy toves\n"
     "Did gyre and gimble in the wabe;\n"
     "  All mimsy were the borogoves,\n"
     "    And the mome raths outgrabe."
     )
</code>


This defines /one/ string value, using compile time concatenation (any adjacent 
string literals are concatenated at compile time, in Python[2] and in C++).

The text was just copied and pasted from Wikipedia, and subjected to a few well 
chosen keystrokes in an editor.

As a hopefully illuminating exercise, consider a Python program that uses this 
string (just import the above module and use its 'text') and generates the above 
source code as output.


Cheers & hth.,

- Alf

Notes:
[1] From an example at the end of chapter 2 at <url: 
http://tinyurl.com/programmingbookP3>.
[2] I'm not sure how well that plays with Python doc strings; haven't tried.



More information about the Python-list mailing list