String problem

Terry Reedy tjreedy at udel.edu
Sat May 25 17:31:10 EDT 2002


"Occean" <tritran2001 at iprimus.com.au> wrote in message
news:20020526.064829.1504569917.16939 at iprimus.com.au...
> Assume that you have an text article.
> "Paint my love, you should paint my love
>   It's a picture of thousand sunset
>
>   It's a freedom of , a thousand doves
>   ......"

If this is a literal multiline string, it must be triple quoted.

> you have to read the article into a string called str

'str()' is a builtin function, now type object.  It is one name you
really, really should not use.  Rebinding builtin names, while legal,
and occasionally useful when you want to replace the builtin, is a bad
habit that you should unlearn now.

>and use that string to pass into the function called
> display(str). This function will display the exact content of the
article back
> into the screen.

'print text' will do the same

>The problem is when you read the article in the string
> you have to put "\n\" at the end of each line in order to let the
system
> know the new line.

Whether you use literal or read in from file, \n's should already be
there.

 For example:
> str = ' Paint my love, you should paint my love\n\
>     It's a picture of thousand sunset \n\
>     \n\
>           It's a freedom of , a thousand doves\n\'
>
> I Just wondering are there another to do that without insert  "\n\"
into
> the end of each line.

Don't shot yourself in the foot by using single quotes for multiline
strings.
Use triple quotes:

mystring ='''\
Paint my love, you should paint my love
It's a picture of a thousand sunsets

It's a freedom of a thousand doves'''

Terry J. Reedy







More information about the Python-list mailing list