Wednesday, January 9, 2013

python dictionary interpolation - wow

I never knew this worked:

>>> fruits = { 'apple' : 1, 'orange':3, 'banana':4 }
>>> print 'I have %(apple)s apple, %(orange)s oranges, and %(banana)s bananas' % fruits
I have 1 apple, 3 oranges, and 4 bananas

Python will take your dictionary and allow you to interpolate it in a string formatting. This leads to some really nice readability.

No comments:

Post a Comment