apostrophe or double quote?

H

Huy

I've been unable to find information clarifying this but. What is the
difference between 'somestring' and "somestring"? When I use type() it
still reports as string. If there is a difference could someone point
me to documentation or explain when to use and when not to? Hope I
sound clear.
 
J

James Stroud

Huy said:
I've been unable to find information clarifying this but. What is the
difference between 'somestring' and "somestring"? When I use type() it
still reports as string. If there is a difference could someone point
me to documentation or explain when to use and when not to? Hope I
sound clear.

There is no difference. However, compare the following:

py> 'internal quotes: "'
'internal quotes: "'
py> "internal apostrophe: '"
"internal apostrophe: '"
py> 'astring'
'astring'
py> "astring"
'astring'
 
S

Steve Holden

Huy said:
I've been unable to find information clarifying this but. What is the
difference between 'somestring' and "somestring"? When I use type() it
still reports as string. If there is a difference could someone point
me to documentation or explain when to use and when not to? Hope I
sound clear.
It's just easier to have two permitted string quotes. That way, if your
string has an apostrophe in it you can say

s = "it's"

and if it has a double quote in it you can say

s = 'The double quote (") rules'

So there's really no difference at all. You can also use escaping to
achieve the same end:

s = "The double quote (\") rules"

if you prefer.

regards
Steve
 
C

Christoph Zwerschke

Steve said:
It's just easier to have two permitted string quotes. That way, if your
string has an apostrophe in it you can say

s = "it's"

It's particularly handy if you are building strings of a language that
already has its own quotes, e.g. SQL or XML:

sql_snippet = " where name='Max'"
html_snippet = ' align="left"'

-- Christoph
 
S

Sion Arrowsmith

Steve Holden said:
It's just easier to have two permitted string quotes. That way, if your
string has an apostrophe in it you can say

s = "it's"

and if it has a double quote in it you can say

s = 'The double quote (") rules'

So there's really no difference at all. You can also use escaping to
achieve the same end:

s = "The double quote (\") rules"

if you prefer.

Or triple quoting:

s = """The double quote (") rules"""


I've seen someone around here use 'somestring' for internal values
(dict keys and the like) and "somestring" for values being shown to
the user, so it's easy(ish) to tell what may need translating or
can otherwise safely be changed. I like this convention (provided
it remains a convention).
 
S

skip

Just to present a complete picture, not mentioned in this thread are
triple-quoted strings:

'abc' == '''abc''' == "abc" == """abc"""

Triple-quoted strings are no different than regular strings, though they do
allow literal newlines to be embedded in the string. Their presence is most
often detected in doc strings precisely for this reason.

Skip
 
T

Terry Hancock

On Wed, 8 Feb 2006 11:57:00 -0600
Just to present a complete picture, not mentioned in this
thread are triple-quoted strings:

'abc' == '''abc''' == "abc" == """abc"""

Triple-quoted strings are no different than regular
strings, though they do allow literal newlines to be
embedded in the string. Their presence is most often
detected in doc strings precisely for this reason.

Also in the mode of beating a dead horse ... ;-)

Some people prefer to use single quotes for 'labels' (i.e. a
name which is meaningful to the program, but not to the
user), and reserve either double-quotes or
triple-double-quotes for text to be shown to the user. This
tends to make things slightly easier when you have to go
back and use gettext to internationalize your code.

But that's a matter of taste.

It is interesting to note, however, that the Python repr()
function prefers to use single quotes, using double quotes
only when a single quote is embedded in the string.

Cheers,
Terry
 
S

Sion Arrowsmith

Terry Hancock said:
Just to present a complete picture, not mentioned in this
thread are triple-quoted strings:
[ ... ]
Also in the mode of beating a dead horse ... ;-)

Some people prefer to use single quotes for 'labels' (i.e. a
name which is meaningful to the program, but not to the
user), and reserve either double-quotes or
triple-double-quotes for text to be shown to the user.
[ ... ]

Hmm, I made both these points a couple of posts upthread, but
it didn't appear to get through the news->mail gateway.
 
T

Terry Hancock

Terry Hancock said:
Just to present a complete picture, not mentioned in
this > thread are triple-quoted strings:
Also in the mode of beating a dead horse ... ;-)

Some people prefer to use single quotes for 'labels'
(i.e. a name which is meaningful to the program, but not
to the user), and reserve either double-quotes or
triple-double-quotes for text to be shown to the user.
[ ... ]

Hmm, I made both these points a couple of posts upthread,
but it didn't appear to get through the news->mail
gateway.

Ah well, it all came up on the list about a month or two ago
anyway (I guess. I don't know, maybe it was a year ago), so
I'm just repeating it.

--
Terry Hancock ([email protected])
Anansi Spaceworks http://www.AnansiSpaceworks.com
 

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,769
Messages
2,569,579
Members
45,053
Latest member
BrodieSola

Latest Threads

Top