Difference between " and '

B

b83503104

Hi,

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

Thanks
 
B

Benjamin Niemann

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"'
 
M

muldoon

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.
 
J

jepler

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)

iD8DBQFC4EawJd01MZaTXX0RAtj2AJ0R7WBfyZbE52R5iRMQ79FUQTeW/QCfQUgI
KZM0i2tJdpoKBeALs/uORQs=
=+5U2
-----END PGP SIGNATURE-----
 
R

Robert Kern

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
(e-mail address removed)

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

Steven D'Aprano

Michael said:
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.
 
R

Robert Kern

Steven said:
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
(e-mail address removed)

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

Benji York

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. :)
 
G

Grant Edwards

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.
 
M

Michael Hoffman

Steven said:
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.
 
P

Peter Hansen

Steven said:
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.
"spaghetti pescatora")
-119

Works for me... ;-)
 
?

=?iso-8859-1?Q?Fran=E7ois?= Pinard

[[email protected]]
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.
 
?

=?iso-8859-1?Q?Fran=E7ois?= Pinard

[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! :)
 
J

John Machin

Peter said:
"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.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
 
T

Terry Hancock

[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.
 
?

=?iso-8859-1?Q?Fran=E7ois?= Pinard

[Terry Hancock]
[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.
 
A

Andrew Dalke

François Pinard said:
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
(e-mail address removed)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,904
Latest member
HealthyVisionsCBDPrice

Latest Threads

Top