assigning multi-line strings to variables

Benjamin Kaplan benjamin.kaplan at case.edu
Tue Apr 27 23:05:14 EDT 2010


On Tue, Apr 27, 2010 at 10:51 PM, goldtech <goldtech at worldpost.com> 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.
> --
>
Neither the '...' or the '>>>' on the other lines have anything to do with
Python, it's just a convenience thing when you're using the interactive
shell. >>> means you're on the first line of an expression and ... means
you're in the middle of a multi-line block. When you actually write scripts,
they aren't there.

$ cat test.py
d = """dddddd
ddddddd
ddddddd
ddddddd"""
print d

$ python test.py
dddddd
ddddddd
ddddddd
ddddddd


http://mail.python.org/mailman/listinfo/python-list
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-list/attachments/20100427/3d6523ed/attachment-0001.html>


More information about the Python-list mailing list