Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #7959

Re: What is this syntax ?

References <4dfdfc99$0$715$426a34cc@news.free.fr>
Date 2011-06-20 00:03 +1000
Subject Re: What is this syntax ?
From Chris Angelico <rosuav@gmail.com>
Newsgroups comp.lang.python
Message-ID <mailman.149.1308492214.1164.python-list@python.org> (permalink)

Show all headers | View raw


On Sun, Jun 19, 2011 at 11:41 PM, candide <candide@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

Back to comp.lang.python | Previous | NextPrevious in thread | Next in thread | Find similar | Unroll thread


Thread

What is this syntax ? candide <candide@free.invalid> - 2011-06-19 15:41 +0200
  Re: What is this syntax ? Laurent Claessens <moky.math@gmail.com> - 2011-06-19 15:50 +0200
  Re: What is this syntax ? Noah Hall <enalicho@gmail.com> - 2011-06-19 14:58 +0100
  Re: What is this syntax ? Chris Angelico <rosuav@gmail.com> - 2011-06-20 00:03 +1000
  Re: What is this syntax ? candide <candide@free.invalid> - 2011-06-19 17:08 +0200
    Re: What is this syntax ? Roy Smith <roy@panix.com> - 2011-06-19 11:39 -0400
      Re: What is this syntax ? rusi <rustompmody@gmail.com> - 2011-06-19 09:58 -0700
        Re: What is this syntax ? Roy Smith <roy@panix.com> - 2011-06-19 16:20 -0400
          Re: What is this syntax ? Vito 'ZeD' De Tullio <zak.mc.kraken@libero.it> - 2011-06-19 23:06 +0200
            Re: What is this syntax ? Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2011-06-19 23:19 +0000
              Re: What is this syntax ? Vito 'ZeD' De Tullio <zak.mc.kraken@libero.it> - 2011-06-20 07:42 +0200
          Re: What is this syntax ? Benjamin Kaplan <benjamin.kaplan@case.edu> - 2011-06-19 15:29 -0700

csiph-web