Python String Substitution

Steven D'Aprano steve at
Sat Jan 28 01:22:21 CET 2006

On Fri, 27 Jan 2006 15:54:30 -0800, Murali wrote:

> No. I dont have a real life example. I was explaining % substitution to
> somebody and realized that I have only used it in the form where the
> keys are strings. Was wondering if there is some special syntax already
> part of python with which I can lookup the dictionary using a tuple as
> a key.

print "%s" % some_dictionary[some_tuple]

Or for that matter:

print "%s" % some_dictionary[any hashable object at all]

What you can't do is use string interpolation like this:

print "%(123)s" % some_dictionary

unless the string "123" is a key in the dictionary. Having a key 123 will
not work, and the same for any other object.


More information about the Python-list mailing list