What is this syntax ?

Chris Angelico rosuav at gmail.com
Sun Jun 19 10:03:31 EDT 2011


On Sun, Jun 19, 2011 at 11:41 PM, candide <candide at free.invalid> wrote:
> With Python 2.7 :
>
>>>> x="foo"
>>>> print '"'+x+'"'
> "foo"
>>>>

As Laurent posted, it's simply a literal double-quote character,
enclosed in single quotes. But for making a quoted string, this isn't
reliable (what if there's a double quote in the string itself?), so
you may want to try repr() which makes a theoretically evalable
string:

>>> x="foo"
>>> print repr(x)
'foo'

>>> x=raw_input()
"Ha ha! 'Tis mine!", he said.
>>> print repr(x)
'"Ha ha! \'Tis mine!", he said.'

In this instance, repr chose to use single quotes, but the same applies.

Chris Angelico



More information about the Python-list mailing list