|
|
|
|
|
by andolanra
5155 days ago
|
|
The new-style formatting allows that as well: >>> "{name} has {count} {thing}".format(
thing='bananas', count=10, name='Phil')
'Phil has 10 bananas'
It also allows for more complex expressions inside of format specifiers, so if you need to grab data out of an object, you can do something like: >>> from collections import namedtuple
>>> Sentence = namedtuple('Sentence', ['name', 'thing', 'count'])
>>> s = Sentence(name='Phil', thing='bananas', count=10)
>>> '{dat[0]} has {dat.count} {dat.thing}'.format(dat=s)
'Phil has 10 bananas'
which is verbose and contrived here, but could be immensely useful in cases like >>> '... {numeral[5]} {item.plural}'.format(
numeral=fr_numerals, item=animal)
'... cinq animaux'
|
|