Where did the "%" operator come from in the context of printing strings all of a sudden?


So I get how everything in this works except the %s in the 6th & 7th line.

`prices = {“banana”: 4,“apple”: 2,“orange”: 1.5,“pear”: 3}

stock = {“banana”: 6, “apple”: 0, “orange”: 32, “pear”: 15}

for food in prices:
print food
print “price: %s” % prices[food]
print “stock: %s” % stock[food]`

How does the modulo operator work in this context? I know what it does but not why and it’s throwing me for a loop (heh).


In this case, it is the string formatting or interpolation operator. I believe %s is for strings and %d is for numbers. The documentation is here if you scroll down to 4.7.2:

There is a “new” way of doing this here:


This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.