473,382 Members | 1,814 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,382 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 1354
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...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

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.