[Tutor] output formatting question

Alan Gauld alan.gauld at btinternet.com
Thu Aug 6 12:19:20 CEST 2015

On 06/08/15 05:08, tom arnall wrote:
> i have read --  with what i think is a reasonable amount of attention
> -- all of the doc' i can find about string formatting in python. for
> the life of me, i cannot see how any of the other methods do more than
> you can do with,
... percent style formatting...

Mostly you are correct, there is not a huge difference in their 
capability and there is little to encourage you to adopt the new style 
other than the fact that it is "preferred" for new code.

However, there are a few things it can do that percent formatting
can't (or at least only with some difficulty). But I'm not the best 
person to list those since I still tend to use % if I'm honest - just 
because its shorter and I'm a C kind of guy so it looks clearer
to me!

You can do some clever stuff with items in a collection.
The docs give this example:

 >>> coord = (3, 5)
 >>> 'X: {0[0]};  Y: {0[1]}'.format(coord)
'X: 3;  Y: 5'

Now for a simple two element example you could do

'X: %d; Y: %d' %(coord[0],coord[1])

But I can see that there might be dynamic cases with
more elements where the new style might be easier

I'm sure there are other similar cases, and others
will supply examples.

Alan G
Author of the Learn to Program web site
Follow my photo-blog on Flickr at:

More information about the Tutor mailing list