Question about quoting style.

S

Steven W. Orr

Python has a number of "quoting" 'options' to help """with
times when""" one way may be more convenient than another.

In the world of shell scripting, I use a technique that I call minimal
quoting. It works like this:

foo=bar # No quotes needed
echo $foo # Also none needed
baz='Hello there' # Don't use double quotes. No iterpolation.
qaz="$baz $foo and grill" # Interpolation needs double quotes.

Is there some sort of best practices approach for when different types of
quoting should be employed?

TIA

--
Time flies like the wind. Fruit flies like a banana. Stranger things have .0.
happened but none stranger than this. Does your driver's license say Organ ..0
Donor?Black holes are where God divided by zero. Listen to me! We are all- 000
individuals! What if this weren't a hypothetical question?
steveo at syslang.net
 
B

Bruno Desthuilliers

Steven W. Orr a écrit :
Python has a number of "quoting" 'options' to help """with
times when""" one way may be more convenient than another.

In the world of shell scripting, I use a technique that I call minimal
quoting. It works like this:

foo=bar # No quotes needed
echo $foo # Also none needed
baz='Hello there' # Don't use double quotes. No iterpolation.
qaz="$baz $foo and grill" # Interpolation needs double quotes.

Is there some sort of best practices approach for when different types
of quoting should be employed?

First point is that Python has no "variable interpolation". Second point
is in Python, quoting style makes no difference wrt escape sequences
(\n, \t etc). Third point is that quoting is not optional when it comes
to string literals - but I guess you knew this one already !-)

Where it makes a difference is that "triple-quoted" string literals
preserves newlines (ok, I guess you knew this too)

IOW, it's mostly a matter of commodity: use double quotes when you don't
want to bother escaping sngle quotes, and vice-versa. As far as I'm
concerned, and since it's quite more common to have a single quote than
a double one in a string literal, I tend to use double-quoted strings by
default. But YMMV !-)
 
C

Carsten Haese

If you squint, it kind of does*:

{'language': "Python", "#": 2}
Python has 002 quote types.

You might think if the dict as a name space and the formatting operation
as performing interpolation--but this take on formatting might be a stretch.

It looks even more like interpolation if you do it like this:
Python has 002 quote types.
 
G

Gabriel Genellina

If you squint, it kind of does*:

{'language': "Python", "#": 2}
Python has 002 quote types.

You might think if the dict as a name space and the formatting operation
as performing interpolation--but this take on formatting might be a
stretch.

Stretching more:

py> language="Python"
py> number=4
py> print '%(language)s has %(number)d quote types.' % locals()
Python has 4 quote types.

Or even more:

py> from string import Template
py> print Template('$language has $number quote
types.').substitute(locals())
Python has 4 quote types.
 
J

J. Cliff Dyer

Bruno said:
Steven W. Orr a écrit :


First point is that Python has no "variable interpolation". Second point
is in Python, quoting style makes no difference wrt escape sequences
(\n, \t etc). Third point is that quoting is not optional when it comes
to string literals - but I guess you knew this one already !-)

Where it makes a difference is that "triple-quoted" string literals
preserves newlines (ok, I guess you knew this too)

IOW, it's mostly a matter of commodity: use double quotes when you don't
want to bother escaping sngle quotes, and vice-versa. As far as I'm
concerned, and since it's quite more common to have a single quote than
a double one in a string literal, I tend to use double-quoted strings by
default. But YMMV !-)
You missed another dimension of python string types.

r'Raw strings are useful when \escaping backslashes. Use them with
regular expressions.'
r"These are also raw. Also use them when talking about escape sequences
like \n"
r"""Same for "\n" Here"""

u'Unicode strings are useful for multilingual or non-latin text.
I\u2019d recommend reading more about Python's unicode handling. It can
be a bit tricky for newcomers.'

Cheers,
Cliff
 
L

Lawrence D'Oliveiro

Steven W. said:
foo=bar # No quotes needed
echo $foo # Also none needed

Actually, it's not clear to me that quotes will never be needed on the
second line. Unless foo is always going to have the value "bar".
 
B

Bruno Desthuilliers

Gabriel Genellina a écrit :
Stretching more:

py> language="Python"
py> number=4
py> print '%(language)s has %(number)d quote types.' % locals()
Python has 4 quote types.

Or even more:

py> from string import Template
py> print Template('$language has $number quote
types.').substitute(locals())
Python has 4 quote types.

Answering to James, Carsten and Gabriel : sorry guys, but I definitively
won't count string formatting as variable interpolation - even if, when
used with locals(), it comes very close from a practical POV (and is
IMHO way cleaner).
 
B

Bruno Desthuilliers

J. Cliff Dyer a écrit :
(snip)
You missed another dimension of python string types.

I didn't "missed" - I choosed to skip the subject since it was not
about quoting style. Not to say I necessarily made the best choice...
 
S

Steve Holden

James said:
If you squint, it kind of does*:

{'language': "Python", "#": 2}
Python has 002 quote types.

You might think if the dict as a name space and the formatting operation
as performing interpolation--but this take on formatting might be a stretch.

James

* http://docs.python.org/lib/typesseq-strings.html

http://docs.python.org/lib/node40.html

regards
Steve
--
Steve Holden +1 571 484 6266 +1 800 494 3119
Holden Web LLC/Ltd http://www.holdenweb.com
Skype: holdenweb http://del.icio.us/steve.holden

Sorry, the dog ate my .sigline
 

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

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,065
Latest member
OrderGreenAcreCBD

Latest Threads

Top