Quotes

Bengt Richter bokr at oz.net
Fri Dec 20 03:01:50 EST 2002


On Fri, 20 Dec 2002 03:35:03 +0000, Tetsuo <member at dbforums.com> wrote:

>
>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:
Depends on how you have "this text" represented
>
>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."
>
I presume somehow you are getting the text into your program as a string,
which I'll simulate by triple quoting the above:

 >>> text = '''"The book is called "Harry Potter and the Philosopher's Stone.""'''

Here is the representation that comes back if you just type the name interactively
 >>> text
 '"The book is called "Harry Potter and the Philosopher\'s Stone.""'

Here you see the printed version just as I quoted it:
 >>> print text
 "The book is called "Harry Potter and the Philosopher's Stone.""

If you know you have quotes at the very beginning and end that you want to get
trid of, you can just slice out what's between, like so:

>>> print text[1:-1]
The book is called "Harry Potter and the Philosopher's Stone."

But if you have such nested quoting with the same quote character
at arbitrary positions in a larger text, that's a different problem,
and you will have to parse the text and count quotes etc. So you will
have to explain further what you need ;-)
>
>
>instead of
>
>
>Traceback (  File "<interactive input>", line 1
>    print "The book is called "Harry Potter and the Philosopher's
>    Stone.""
>                                   ^
>SyntaxError: invalid syntax
>
>--
>Posted via http://dbforums.com

Regards,
Bengt Richter



More information about the Python-list mailing list