It is troublesome to look at the document again, so make a note of only the necessary parts.
{: <number}{:> numbers}{: ^ Numbers}{: padded characters ^ numbers} >>> '{:<30}'.format('left aligned')
'left aligned                  '
>>> '{:>30}'.format('right aligned')
'                 right aligned'
>>> '{:^30}'.format('centered')
'           centered           '
>>> '{:*^30}'.format('centered')
'***********centered***********'
{: d}{: x} or {: # x}{: b} or {: # b}>>> "int: {0:d};  hex: {0:x};  oct: {0:o};  bin: {0:b}".format(42)
'int: 42;  hex: 2a;  oct: 52;  bin: 101010'
>>> "int: {0:d};  hex: {0:#x};  oct: {0:#o};  bin: {0:#b}".format(42)
'int: 42;  hex: 0x2a;  oct: 0o52;  bin: 0b101010'
>>> "{:,}".format(1000000)
'1,000,000'
>>> urldata = {"scheme":"http","netloc":"qiita.com","path":"/drafts"}
>>> url = "{scheme}://{netloc}{path}".format(urldata)
** 0. ** is important.
>>> url = "{0.scheme}://{0.netloc}{0.path}".format(urldata)
>>> import datetime
>>> d = datetime.datetime(2010, 7, 4, 12, 15, 58)
>>> '{:%Y-%m-%d %H:%M:%S}'.format(d)
'2010-07-04 12:15:58'
Recommended Posts