assigning multi-line strings to variables

MRAB python at mrabarnett.plus.com
Wed Apr 28 05:06:51 CEST 2010


goldtech wrote:
> On Apr 27, 7:33 pm, MRAB <pyt... at mrabarnett.plus.com> wrote:
>> goldtech 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
>> Use a triple-quoted string literal:
>>
>>  >>> d = """ddddd
>> ... ddddd"""
>>  >>> d
>> 'ddddd\nddddd'
> 
> Only seems to work when there's a '... ' on the 2nd line. I need a way
> to assign large blocks of text to a variable w/out special formatting.
> Thanks.

I copied it from an interactive session in IDLE. In an actual script I
would write:

text = """first line
second line
third line"""



More information about the Python-list mailing list