[Tutor] %d

D-Man dsh8290@rit.edu
Tue, 22 May 2001 17:25:28 -0400


On Tue, May 22, 2001 at 09:58:17PM +0100, Patrick Kirk wrote:
| In Alan Gauld's tutorial page 2 you suddenly move from gentle hand holding
| to this:
| 
| >>>print "The sum of %d and %d is: %d" % (v,w,x)
| 
| v,w,x are variables and have integer values.  d is less clear.  And %?
| 
| I thought % was for the modulus function.  So what is that % doing in front
| of d which I assume to be a built in variable?  And what is the % in front
| of the curly brackets?

The % operator does several different things, depending on the
context.  It does modulo arithmatic if it is applied to integer
objects such as

print 5 % 2


If it is applied with a string as the left hand side it performs
string interpolation.  To understand string interpolation one must
become familiar with "format strings".  There is brief mention of
format strings in the documentation with regard to the print
statement.  It basically says that it behaves the same way C's printf
works.  (you can try reading 'man printf' if you have a Unix system
and want to see some terse documentation)  IIRC you have a book on
C++.  You should be able to find some info regarding printf/scanf and
format strings in there.  A _brief_ (and shallow) introduction follows
:


The '%d' construct you see isn't actually python code -- it is inside
a string.  This particular string is called a "format string" because
it contains markup that indicates how the text should be formatted.
Each markup code begins with a '%' sign such as '%d'.  The 'd'
indicates that the value provided (in python through the use of string
interpolation) should be formatted as an integer (decimal? digit?).
It will be right aligned and use only as much space as is needed for
all the digits.  A number can follow the % and precede the d to
specify how the text should be aligned.  For example :

%2d         Right aligned, 2 characters for field width 
%-2d        Left  aligned,  2 characters for field width
%10.1f      Right aligned float, 10 characters for field width, 1
                character for precision
%-10.1f     Left  aligned float, 10 characters for field width, 1
                character for precision
%x          A hexadecimal number
%#x         A hexadecimal number preceded by 0x, uses abcdef
%#X         A hexadecimal number preceded by 0X, uses ABCDEF
%s          A string


Here are a few examples :

>>> print "'%-10.1f'" % 987.65
'987.6     '
>>> print "'%10.1f'" % 987.65
'     987.6'
>>> print "'%x'" % 987
'3db'
>>> print "'%#x'" % 987
'0x3db'
>>> print "'%#X'" % 987
'0X3DB'
>>>



This might be too much information, but in Python string interpolation
can handle interpolating with a dict:

>>> d = { "first" : "Hello World" , "second" : "Python Is Cool" }
>>> print "First he said '%(first)s' then he said '%(second)s'" % d
First he said 'Hello World' then he said 'Python Is Cool'

The parenthesized text in between the '%' and the 's' is the key for
the dictionary.


HTH,
-D