RE: Python: Please Help Understanding String Formatting Syntax

  • From: "Homme, James" <james.homme@xxxxxxxxxxxx>
  • To: "programmingblind@xxxxxxxxxxxxx" <programmingblind@xxxxxxxxxxxxx>
  • Date: Wed, 3 Aug 2011 06:39:46 -0400

Hi,
Oh, I see now. The thing in parentheses is the name of the variable. How did I 
miss that?

Jim

-----Original Message-----
From: programmingblind-bounce@xxxxxxxxxxxxx 
[mailto:programmingblind-bounce@xxxxxxxxxxxxx] On Behalf Of Tyler Spivey
Sent: Tuesday, August 02, 2011 3:33 PM
To: programmingblind@xxxxxxxxxxxxx
Subject: Re: Python: Please Help Understanding String Formatting Syntax

It's at:
http://docs.python.org/library/stdtypes.html#string-formatting
Specificly these parts:
If format requires a single argument, values may be a single non-tuple object.
{[4]} Otherwise, values must be a tuple with exactly the number of
items specified by the format string, or a single mapping object (for example, 
a dictionary). 
...
A conversion specifier contains two or more characters and has the following
components, which must occur in this order:
1. The '%' character, which marks the start of the specifier.
2. Mapping key (optional), consisting of a parenthesised sequence of characters
(for example, (somename)). 

In one of my scripts, I have this line:
print "%.2f%%: %d/%d" % (pct(i+1, total), i+1, total)
I could get the same effect by doing this:
print "%(percentage).2f%%: %(current)d/%(total)d" % {
"percentage":pct(i+1, total), 
"current": i+1, "total":total}

On 8/2/2011 10:22 AM, Homme, James wrote:
> Hi,
> I understand what I have read about string formatting so far in the Python 
> Standard Library documentation, namely, I understand about using the % sign 
> and various letters, then using either a single string or a tuple to specify 
> what to substitute, but I don't understand some things about the below string 
> formatting. This piece of text is from the book The Python Standard Library 
> By Example, and talks about the ConfigParser module. Specifically, I don't 
> understand why it excepts the formatting without quotes, and I'm guessing 
> that the s after the  parentheses means that the various pieces are strings.  
> I can see that it's grabbing values from the key-value pares. In my reading, 
> though, about string formatting, I haven't seen this particular syntax. Where 
> is it in the Python docs?
> 
> See below.
> 
> 14.8.7. Combining Values with Interpolation
> 
> SafeConfigParser provides a feature called interpolation
> that can be used to combine values together. Values containing standard 
> Python format strings trigger the interpolation feature when they are 
> retrieved
> with get().
> Options named within the value being fetched are replaced with their values 
> in turn, until no more substitution is necessary.
> 
> ... For example, this configuration file separates the protocol, hostname, 
> and port from the URL as separate options.
> 
> [bug_tracker]
> protocol = http
> server = localhost
> port = 8080
> url = %(protocol)s://%(server)s:%(port)s/bugs/
> 
> Thanks.
> 
> Jim
> Jim Homme,
> Usability Services,
> Phone: 412-544-1810.
> 
> 
> ________________________________
> This e-mail and any attachments to it are confidential and are intended 
> solely for use of the individual or entity to whom they are addressed. If you 
> have received this e-mail in error, please notify the sender immediately and 
> then delete it. If you are not the intended recipient, you must not keep, 
> use, disclose, copy or distribute this e-mail without the author's prior 
> permission. The views expressed in this e-mail message do not necessarily 
> represent the views of Highmark Inc., its subsidiaries, or affiliates.
> 

__________
View the list's information and change your settings at 
//www.freelists.org/list/programmingblind

__________
View the list's information and change your settings at
//www.freelists.org/list/programmingblind

Other related posts: