Quotes

John Roth johnroth at ameritech.net
Fri Dec 20 17:23:34 EST 2002


"Tetsuo" <member at dbforums.com> wrote in message
news:2304302.1040355303 at dbforums.com...
>
> Well, I'm making this Web-based program that I have mentioned and that
> 99% of you have never heard about. I finally thought up a name for it
> and even started making the first file, after months of sitting around
> and/or trying to get things to work.
>
> I have another problem. What I really need would take a long
> explanation, but I guess the answer to this question will be
sufficient.
> I thought up some crap with chr(34) and chr(39), but it seems too
> confusing to be really necessary. Surely there's an easy solution to
> such an elementary problem.
>
>
>
> Let's say I need to have this text printed:
>
> print "The book is called "Harry Potter and the Philosopher's Stone.""
>
> How do I get the user to see
>
>
> The book is called "Harry Potter and the Philosopher's Stone."
>
>
>
> instead of
>
>
> Traceback (  File "<interactive input>", line 1
>     print "The book is called "Harry Potter and the Philosopher's
>     Stone.""
>                                    ^
> SyntaxError: invalid syntax

Don't use a literal. The root of the problem is the confusion between
the quote as a string delimiter, and the quote as a printable character.
If you read the string in from somewhere, and then print it, you're
not going to have this problem.

If you absolutely have to have it as a literal somewhere, use the
suggestion someone else made, with multi-line quotes, and then
use the .strip() function to remove extra whitespace from both
ends.

John Roth






More information about the Python-list mailing list