标签:
23 42
tree: oak
two one
data = {‘first‘: ‘Hodor‘, ‘last‘: ‘Hodor!‘}
‘%(first)s %(last)s‘ % data
‘{first} {last}‘.format(**data)
Hodor Hodor!
‘{:.5}‘.format(‘xylophone‘)
xylop
‘{:^10}‘.format(‘test‘)
test
‘{:.{}}‘.format(‘xylophone‘, 7)
xylopho
‘{:4d}‘.format(42)
42
‘{:06.2f}‘.format(3.141592653589793)
003.14
‘{:+d}‘.format(42)
+42
person = {‘first‘: ‘Jean-Luc‘, ‘last‘: ‘Picard‘}
‘{p[first]} {p[last]}‘.format(p=person)
Jean-Luc Picard
data = [4, 8, 15, 16, 23, 42]
‘{d[4]} {d[5]}‘.format(d=data)
23 42
class Plant(object): type = ‘tree‘ kinds = [{‘name‘: ‘oak‘}, {‘name‘: ‘maple‘}]
‘{p.type}: {p.kinds[0][name]}‘.format(p=Plant())
tree: oak
class Data(object): def __str__(self): return ‘str‘ def __repr__(self): return ‘repr‘
‘%s %r‘ % (Data(), Data())
‘{0!s} {0!r}‘.format(Data())
str repr
class HAL9000(object): def __format__(self, format): if (format == ‘open-the-pod-bay-doors‘): return "I‘m afraid I can‘t do that." return ‘HAL 9000‘
‘{:open-the-pod-bay-doors}‘.format(HAL9000())
I‘m afraid I can‘t do that.
from datetime import datetime
‘{:%Y-%m-%d %H:%M}‘.format(datetime(2001, 2, 3, 4, 5))
2001-02-03 04:05
标签:
原文地址:http://www.cnblogs.com/ToDoToTry/p/5635863.html