By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
440,592 Members | 1,931 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 440,592 IT Pros & Developers. It's quick & easy.

Difference between " and '

P: n/a
Hi,

Can someone tell me the difference between single quote and double
quote?

Thanks

Jul 21 '05 #1
Share this Question
Share on Google+
21 Replies


P: n/a
b8*******@yahoo.com wrote:
Hi,

Can someone tell me the difference between single quote and double
quote?

There is none. Except that in a double quoted string, single quotes don't
have to be escaped and vice versa, sometimes one of the two forms saves you
some backslashes:

"That's my house" == 'That\'s my house'
"You say: \"Hello\"" == 'You say: "Hello"'
--
Benjamin Niemann
Email: pink at odahoda dot de
WWW: http://www.odahoda.de/
Jul 21 '05 #2

P: n/a
b8*******@yahoo.com wrote:
Hi,

Can someone tell me the difference between single quote and double
quote?


ord("'") - ord('"')

5

or ask a meaningful question ...

Jul 21 '05 #3

P: n/a

b8*******@yahoo.com wrote:
Hi,

Can someone tell me the difference between single quote and double
quote?

Thanks


And please settle the dispute between "xxx". And "xxx".

There was a fellow at Oxford who decided these things but I hear he
went mad.

Jul 21 '05 #4

P: n/a
John Machin wrote:
b8*******@yahoo.com wrote:
Can someone tell me the difference between single quote and double
quote?

>>> ord("'") - ord('"')

5


Very zen.
--
Michael Hoffman
Jul 21 '05 #5

P: n/a
The only difference is when you want to include " or ' inside the string. If
you want to include the "like" quote, then escape it ("\"", '\''). If you
include the "unlike" quote, no escape is needed ("'" or '"').

I think that people new to programming will use '' if it is unshifted on their
keyboards. People from a "C" background may use "" for strings (since in that
language, '' is for a single character only).

Jeff

-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.2.6 (GNU/Linux)

iD8DBQFC4EawJd01MZaTXX0RAtj2AJ0R7WBfyZbE52R5iRMQ79 FUQTeW/QCfQUgI
KZM0i2tJdpoKBeALs/uORQs=
=+5U2
-----END PGP SIGNATURE-----

Jul 22 '05 #6

P: n/a
je****@unpythonic.net wrote:
The only difference is when you want to include " or ' inside the string. If
you want to include the "like" quote, then escape it ("\"", '\''). If you
include the "unlike" quote, no escape is needed ("'" or '"').

I think that people new to programming will use '' if it is unshifted on their
keyboards. People from a "C" background may use "" for strings (since in that
language, '' is for a single character only).


I find that I switch between the two pretty freely. One habit that seems
to crop up, though, is that I will use '' for internal strings and ""
for strings that will eventually get seen by the user.

Don't ask me why.

--
Robert Kern
rk***@ucsd.edu

"In the fields of hell where the grass grows high
Are the graves of dreams allowed to die."
-- Richard Harter

Jul 22 '05 #7

P: n/a
Michael Hoffman wrote:
John Machin wrote:
b8*******@yahoo.com wrote:
Can someone tell me the difference between single quote and double
quote?

>>> ord("'") - ord('"')

5


Very zen.

But unfortunately incorrect, since the original poster
didn't ask for the difference between the ordinal
values of the single quote and double quote characters,
but for the difference between the characters themselves.

The question was in plain English, and in my opinion,
perfectly understandable, especially since some common
programming languages do have significant differences
between single-quoted strings and double-quoted strings.

It may shock some people to learn that difference in
the sense of mathematical subtraction is not the only
meaning of the word, but there it is. One wouldn't, I
hope, misunderstand "What is the difference between
spaghetti marinara and spaghetti pescatora?" and
attempt to subtract one from the other, since
subtraction is not defined for foodstuffs.

--
Steven.

Jul 22 '05 #8

P: n/a
Steven D'Aprano wrote:
Michael Hoffman wrote:
John Machin wrote:
b8*******@yahoo.com wrote:

Can someone tell me the difference between single quote and double
quote?

>>> ord("'") - ord('"')
5


Very zen.


But unfortunately incorrect, since the original poster
didn't ask for the difference between the ordinal
values of the single quote and double quote characters,
but for the difference between the characters themselves.


It was a joke, I believe, not a serious answer.

--
Robert Kern
rk***@ucsd.edu

"In the fields of hell where the grass grows high
Are the graves of dreams allowed to die."
-- Richard Harter

Jul 22 '05 #9

P: n/a
Steven D'Aprano wrote:
Michael Hoffman wrote:
John Machin wrote:
b8*******@yahoo.com wrote:
Can someone tell me the difference between single quote and double
quote? >>> ord("'") - ord('"')
5
Very zen.
But unfortunately incorrect, since the original poster
didn't ask for the difference between the ordinal
values of the single quote and double quote characters


Steven, your reply was one of the most subtle and hilarious things I've
read in a long time. Unfortunately I couldn't tell if it was
intentional or not. :)
--
Benji York
Jul 22 '05 #10

P: n/a
On 2005-07-22, Steven D'Aprano <st***@REMOVEMEcyber.com.au> wrote:
Can someone tell me the difference between single quote and double
quote?

>>> ord("'") - ord('"')
5


Very zen.


But unfortunately incorrect, since the original poster
didn't ask for the difference between the ordinal
values of the single quote and double quote characters,

[...]

It's a, I say it's a _joke_ son. Don't cha get it? You're
built too short, the good ones go over yer head. Ya got a
hole in yer glove, boy, I keep pitching them and you keep
missing them. Ya gotta keep yer eye on the ball, son. Eye.
Ball. Eyeball. I almost had a funny there. Joke, that is.
--
Grant Edwards grante Yow! Gibble, Gobble, we
at ACCEPT YOU...
visi.com
Jul 22 '05 #11

P: n/a
b8*******@yahoo.com wrote:
Can someone tell me the difference between single quote and double
quote?


One has double the fun.

--
Erik Max Francis && ma*@alcyone.com && http://www.alcyone.com/max/
San Jose, CA, USA && 37 20 N 121 53 W && AIM erikmaxfrancis
Forgive your enemies, but never forget their names.
-- John F. Kennedy
Jul 22 '05 #12

P: n/a
Steven D'Aprano wrote:
Michael Hoffman wrote:
John Machin wrote:
b8*******@yahoo.com wrote:

Can someone tell me the difference between single quote and double
quote?

>>> ord("'") - ord('"')
5


Very zen.


But unfortunately incorrect, since the original poster didn't ask for
the difference between the ordinal values of the single quote and double
quote characters, but for the difference between the characters themselves.


It reminded me of a zen koan, because although it was a joke at the
expense of original poster, the true answer was still contained within
when the answerer had to use different kinds of quoting to quote single
and double quotes.
--
Michael Hoffman
Jul 22 '05 #13

P: n/a
Steven D'Aprano wrote:
It may shock some people to learn that difference in the sense of
mathematical subtraction is not the only meaning of the word, but there
it is. One wouldn't, I hope, misunderstand "What is the difference
between spaghetti marinara and spaghetti pescatora?" and attempt to
subtract one from the other, since subtraction is not defined for
foodstuffs.

sum(ord(c) for c in 'spaghetti marinara') - sum(ord(c) for c in

"spaghetti pescatora")
-119

Works for me... ;-)
Jul 22 '05 #14

P: n/a
[b8*******@yahoo.com]
Can someone tell me the difference between single quote and double
quote?


There is no strong reason to use one and avoid the other. Yet, while
representing strings, Python itself has a _preference_ for single
quotes. Programmers can put this duality to good use, by adopting
reasonable conventions, which are nothing more than conventions! :-)

I would guess many people choose one over the other for minimizing the
amount of backslash escape needed. Some people keep double quotes for
strings that would later undergo formatting, maybe because in most
shells and some other languages, double quotes allow for substitution
and single quotes prevent it. But in my opinion, while not dismissing
a lot of wisdom developed within other languages, it may be foolish
letting other languages blindly dictate what is best Python style.

Personally, I keep single quotes for "computer" strings, and double
quotes for "human" strings. To segregate between "computer" and "human"
character for a string, I merely ask myself: "If I wanted to use this
program in another language, would I want this particular string
translated or not?". Yes means double quotes, no means single quotes.
As single quotes are often use within human text, as apostrophes, this
was a wise choice as far as shell escaping goes.

This is extendable to triple-quoted strings. I use triple-double quotes
for doc strings meant to hold documentation, which is the usual case.
If doc strings are used for BNF snippets, like with SPARK, or any other
machine data, triple-single quotes are mandatory by my own convention.

--
François Pinard http://pinard.progiciels-bpi.ca
Jul 22 '05 #15

P: n/a
[Robert Kern]
One habit that seems to crop up, though, is that I will use '' for
internal strings and "" for strings that will eventually get seen by
the user. Don't ask me why.


One sure thing is that it would help, later, if you ever want to
internationalise a Python program. Not that it occurs that often! :-)

--
François Pinard http://pinard.progiciels-bpi.ca
Jul 22 '05 #16

P: n/a
Peter Hansen wrote:
Steven D'Aprano wrote:
It may shock some people to learn that difference in the sense of
mathematical subtraction is not the only meaning of the word, but
there it is. One wouldn't, I hope, misunderstand "What is the
difference between spaghetti marinara and spaghetti pescatora?" and
attempt to subtract one from the other, since subtraction is not
defined for foodstuffs.


>>> sum(ord(c) for c in 'spaghetti marinara') - sum(ord(c) for c in "spaghetti pescatora")
-119

Works for me... ;-)


Works for me too:

C:\junk>python --fuzzy --punt
Python 9.4.1 (#165, Mar 30 3005, 09:13:57) [MSC v.13109 1024 bit
(Intel)] on win1024
Type "help", "copyright", "credits" or "license" for more information.
a = foodstuff('spaghetti marinara')
b = foodstuff('spaghetti pescatora')
a - b 0 explain

a.base is b.base
a.topping != b.topping
but
fuzzy mode selected
a.topping isa salty_smelly_stuff
b.topping isa salty_smelly_stuff
punt mode selected
prob(customer_knows_difference) = 0.01
PSF interdiction
all your operator are belong to us
Jul 22 '05 #17

P: n/a
On Friday 22 July 2005 08:09 am, François Pinard wrote:
[Robert Kern]
One habit that seems to crop up, though, is that I will use '' for
internal strings and "" for strings that will eventually get seen by
the user. Don't ask me why.


One sure thing is that it would help, later, if you ever want to
internationalise a Python program. Not that it occurs that often! :-)


Whoa. Why? Does xgettext not recognize _('')? I'm just learning
to use it. Maybe I should pay attention to this point if that's so.

--
Terry Hancock ( hancock at anansispaceworks.com )
Anansi Spaceworks http://www.anansispaceworks.com

Jul 22 '05 #18

P: n/a
[Terry Hancock]
On Friday 22 July 2005 08:09 am, François Pinard wrote:
[Robert Kern]
One habit that seems to crop up, though, is that I will use '' for
internal strings and "" for strings that will eventually get seen
by the user. Don't ask me why.
One sure thing is that it would help, later, if you ever want to
internationalise a Python program. Not that it occurs that often! :-)
Whoa. Why? Does xgettext not recognize _('')?


Yes, of course.

The most tedious part of internationalising a project is the selection
of strings to be translated, among all program strings, and wrapping
these within the special _(...) construct. If you can _already_ rely
on the fact double-quote strings are always to be wrapped, and that
single-quote strings are never to be wrapped, the tedious part is
already done, or almost. A few relatively quick editor commands are
may complete that aspect of the work (provided you use a reasonably
featureful editor).

As for Using xgettext or its Python counterpart, this is usually burned
into the release engineering (Makefile, distutils, or else), and is not
much of an effort, once you found a good recipe or example for it.

--
François Pinard http://pinard.progiciels-bpi.ca
Jul 22 '05 #19

P: n/a
François Pinard wrote:
There is no strong reason to use one and avoid the other. Yet, while
representing strings, Python itself has a _preference_ for single
quotes.


I use "double quoted strings" in almost all cases because I
think it's easier to see than 'single quoted quotes'.

Andrew
da***@dalkescientific.com

Jul 22 '05 #20

P: n/a
Andrew Dalke said unto the world upon 2005-07-22 13:30:
François Pinard wrote:
There is no strong reason to use one and avoid the other. Yet, while
representing strings, Python itself has a _preference_ for single
quotes.

I use "double quoted strings" in almost all cases because I
think it's easier to see than 'single quoted quotes'.

Andrew
da***@dalkescientific.com


I prefer the look of '''single triple''' to """double triple""" but a
while back I noticed pydoc doesn't work as well with single triple
quotes. When I posted here, Kent Johnson was kind enough to tease out
of me what I was on about and file a bug:
<http://sourceforge.net/tracker/index.php?func=detail&aid=1185124&group_id=5470&at id=105470>

Best to all,

Brian vdB

Jul 22 '05 #21

P: n/a
On Fri, 22 Jul 2005 08:38:28 -0400, Peter Hansen wrote:
Steven D'Aprano wrote:
It may shock some people to learn that difference in the sense of
mathematical subtraction is not the only meaning of the word, but there
it is. One wouldn't, I hope, misunderstand "What is the difference
between spaghetti marinara and spaghetti pescatora?" and attempt to
subtract one from the other, since subtraction is not defined for
foodstuffs.

>>> sum(ord(c) for c in 'spaghetti marinara') - sum(ord(c) for c in "spaghetti pescatora")
-119

Works for me... ;-)


Yes, but if you had thought about the problem a little longer, you would
have removed the spaghetti from both, since they just cancel each other.
Although that does assume that the commutative law holds for pasta
products.

Personally, I prefer this answer:
import sets
sets.Set("spaghetti marinara") - sets.Set("spaghetti pescatora")

Set(['m', 'n'])

--
Steven.
Jul 23 '05 #22

This discussion thread is closed

Replies have been disabled for this discussion.