471,312 Members | 1,828 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,312 software developers and data experts.

Question about quoting style.

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
Oct 1 '07 #1
7 1277
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 !-)

Oct 1 '07 #2
Bruno Desthuilliers wrote:
First point is that Python has no "variable interpolation".
If you squint, it kind of does*:
>>print '%(language)s has %(#)03d quote types.' % \
{'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
Oct 1 '07 #3
On Mon, 2007-10-01 at 15:10 -0700, James Stroud wrote:
Bruno Desthuilliers wrote:
First point is that Python has no "variable interpolation".

If you squint, it kind of does*:
>>print '%(language)s has %(#)03d quote types.' % \
{'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:
>>language = "Python"
num = 2
print '%(language)s has %(num)03d quote types.' % locals()
Python has 002 quote types.

--
Carsten Haese
http://informixdb.sourceforge.net
Oct 1 '07 #4
Bruno Desthuilliers wrote:
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 !-)

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
Oct 2 '07 #5
In message <ma**************************************@python.o rg>, Steven W.
Orr wrote:
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".
Oct 2 '07 #6
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...

Oct 2 '07 #7
James Stroud wrote:
Bruno Desthuilliers wrote:
>First point is that Python has no "variable interpolation".

If you squint, it kind of does*:
>>print '%(language)s has %(#)03d quote types.' % \
{'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

Oct 3 '07 #8

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

7 posts views Thread by Porky Pig Jr | last post: by
55 posts views Thread by ben | last post: by
2 posts views Thread by leegold2 | last post: by
104 posts views Thread by Colin McGuire | last post: by
105 posts views Thread by Christoph Zwerschke | last post: by
10 posts views Thread by Michael Hoffman | last post: by
2 posts views Thread by mathieu | last post: by
3 posts views Thread by Evan | last post: by
reply views Thread by rosydwin | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.