% string formatting - what special method is used for %d?

Veek M vek.m1234 at gmail.com
Sat Dec 10 02:06:23 EST 2016


When we do:

print '%s %d' % ('hello', 10)

what special method is being invoked internally within the string-
format-specifier? 

format() invokes format__
print invokes __str__

I'm basically trying to make sense of:

raise TypeError('urkle urkle %s' % list(dictionary))
<=> raise TypeError('urkle urkle %s' % [ key1, val1, key2, val2 ]

So, the % operator reads the format specifier and notices %s and 
therefore calls __str__ in the list class to figure out how to represent 
[ key1, val1, key2, val2 ].

However what if I use %d? How do the other format specs work?


More information about the Python-list mailing list