What does %s and % do in Keeping Track of the Produce?


#1

Keeping Track of the Produce

What does %s and % do in Keeping Track of the Produce? Why are they needed?

prices = {
    "banana": 4,
    "apple": 2,
    "orange": 1.5,
    "pear": 3
    }
    
stock = {
    "banana": 6,
    "apple": 0,
    "orange": 32,
    "pear": 15
    }
    
for key in prices:
    print key
    print "price: %s" % prices[key]
    print "stock: %s" %stock[key]

#2

This is a string formatting operator derived from C actually,

Meaning if I were to have a code like this,

my_name = "Dave"
print("My name is, %s"% (my_name))

%s fetches the string located in my variable using the modulus as a pointer, % (my_name))


#3

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