difference between %s and %d

MRAB python at mrabarnett.plus.com
Fri Oct 1 15:38:52 EDT 2010


On 01/10/2010 20:15, Ethan Furman wrote:
> If I'm printing the number 735, or any other positive or negative
> integer, is there any difference between %7s and %7d?
>
"%s" uses str(). In Python 2.7:

 >>> class MyInt(int):
...     def __init__(self, value):
...         int.__init__(self, value)
...     def __str__(self):
...         return "MyInt(%d)" % self
...
 >>> MyInt(735)
735
 >>> "%7d" % MyInt(735)
'    735'
 >>> "%7s" % MyInt(735)
'MyInt(735)'



More information about the Python-list mailing list