473,230 Members | 1,381 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,230 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 1350
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

7
by: Porky Pig Jr | last post by:
Hello, I"m still learning Python, but going through the Ch 5 OOP of Nutshell book. There is discussion on __slots__, and my understanding from reading this section is that if I have a class...
383
by: John Bailo | last post by:
The war of the OSes was won a long time ago. Unix has always been, and will continue to be, the Server OS in the form of Linux. Microsoft struggled mightily to win that battle -- creating a...
55
by: ben | last post by:
is it true that a function without an inline keyword never get inlined? If not true when is it inlined or not? ben
2
by: leegold2 | last post by:
I wondered if anyone would give me code- I think it would be easy, but I'm a complete newbie. What I want to do is to show many tables in a brief truncated format and then for each table offer the...
104
by: Colin McGuire | last post by:
Hi, is there a way to show a form without a titlebar (and therefore no control box/minimize box/title etc) but still have it appear looking like 3D? The property FormBorderStyle to None - this...
105
by: Christoph Zwerschke | last post by:
Sometimes I find myself stumbling over Python issues which have to do with what I perceive as a lack of orthogonality. For instance, I just wanted to use the index() method on a tuple which does...
10
by: Michael Hoffman | last post by:
Does anyone have a script to convert more conventional USENET quoting style like this: John wrote: > Jacob Jingleheimer Schmidt <jjs@example.net> wrote in message-id <gratuitous-detail> on 29...
2
by: mathieu | last post by:
Hi I was struggling with a weird path. So I decided to use a fancy option in ls to help me out. But I think the output is incorrect. Steps: mkdir /tmp/bla touch "/tmp/bla/filename with |...
3
by: Evan | last post by:
a simple problem but I do not know why...:(, could anyone help me? MySQLdb nominally uses just the %s placeholder style, in my script, i got error if you want to use placeholder(%s) for table...
0
by: abbasky | last post by:
### Vandf component communication method one: data sharing ​ Vandf components can achieve data exchange through data sharing, state sharing, events, and other methods. Vandf's data exchange method...
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 7 Feb 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:30 (7.30PM). In this month's session, the creator of the excellent VBE...
0
by: fareedcanada | last post by:
Hello I am trying to split number on their count. suppose i have 121314151617 (12cnt) then number should be split like 12,13,14,15,16,17 and if 11314151617 (11cnt) then should be split like...
0
by: stefan129 | last post by:
Hey forum members, I'm exploring options for SSL certificates for multiple domains. Has anyone had experience with multi-domain SSL certificates? Any recommendations on reliable providers or specific...
0
by: MeoLessi9 | last post by:
I have VirtualBox installed on Windows 11 and now I would like to install Kali on a virtual machine. However, on the official website, I see two options: "Installer images" and "Virtual machines"....
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: Aftab Ahmad | last post by:
Hello Experts! I have written a code in MS Access for a cmd called "WhatsApp Message" to open WhatsApp using that very code but the problem is that it gives a popup message everytime I clicked on...
0
by: Aftab Ahmad | last post by:
So, I have written a code for a cmd called "Send WhatsApp Message" to open and send WhatsApp messaage. The code is given below. Dim IE As Object Set IE =...

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.