View Single Post
fpp's Avatar
Posts: 2,853 | Thanked: 968 times | Joined on Nov 2005
#42
Originally Posted by TrueJournals View Post
I hate to be a nit-picker here, but... daperl: from my understanding of python (and I could be wrong, I'm still learning), items in parentheses () are a tuple, while items in brackets [] are a list. So, in this case, the items need to be in a tuple, not a list. Not a big deal here, but the terminology changes what you can do with the items. (Reference Link)
You're quite right. Although tuples are, in effect, like immutable lists, they're not equivalent in this case.

The other data structure commonly used for string formatting is the Python 'dictionary', especially when there are many variables and/or some of them are used multiple times. The placeholders are then named after the dict keys and their type, as in :

Code:
values = {'name':'Joe', 'score':10, 'rank':'first'}
print "%(name)s scored %(score)d and ranked %(rank)s, congrats %(name)s !" % values
A commonly used dict is the one returned by locals(), which contains all local variables in context.
 

The Following 2 Users Say Thank You to fpp For This Useful Post: