From: geremy condra on
On Tue, Apr 27, 2010 at 10:51 PM, goldtech <goldtech(a)worldpost.com> wrote:
> On Apr 27, 7:33 pm, MRAB <pyt...(a)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.

The '...' is the interpreter's way of telling you that you are in a
block, e.g. a function definition, with statement, etc.

The '\n' you see in the middle of the string is the newline
character. If you try to print the string, the expected
happens:

>>> d = """dddddddddddddddd
.... ddddddddddddddddddddddd"""
>>> d
'dddddddddddddddd\nddddddddddddddddddddddd'
>>> print(d)
dddddddddddddddd
ddddddddddddddddddddddd

Geremy Condra
From: MRAB on
goldtech wrote:
> On Apr 27, 7:33 pm, MRAB <pyt...(a)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"""
From: Ben Finney on
goldtech <goldtech(a)worldpost.com> writes:

> Only seems to work when there's a '... ' on the 2nd line.

You seem to be confused by the presentation of the interactive
interpreter. That text is a prompt.

> I need a way to assign large blocks of text to a variable w/out
> special formatting.

That's what triple-quoted string syntax is for.

You would do well to work through the entirety of the Python tutorial
<URL:http://docs.python.org/tutorial/>. Take the time to actually
perform each exercise and experiment until you understand the concept
being presented, before moving on.

--
\ “I find the whole business of religion profoundly interesting. |
`\ But it does mystify me that otherwise intelligent people take |
_o__) it seriously.” —Douglas Adams |
Ben Finney
From: Sagar K on
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?


From: cjw on
On 27-Apr-10 22:31 PM, Brendan Abel 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.
What about:
*** Python 2.6.4 (r264:75708, Oct 26 2009, 08:23:19) [MSC v.1500 32 bit
(Intel)] on win32. ***
>>> a= ''' Now is the time for
.... all good men
.... to come to the aid
.... of the party'''
>>> print (a)
Now is the time for
all good men
to come to the aid
of the party
>>>

Colin W.