What does this ( [[fill]align]) syntax mean?
From the format specification mini language https://docs.python.org/2/library/string.html#format-specification-mini-language
Does it mean if you have a fill then you must have an align?
I was trying -
In [71]: '{:{}{}}'.format('test','.','10')
Out[72]: 'test'
I think that has a fill(character) '.' and width ('10'). I used python 3.6.3.
I think perhaps you want to look the the examples here .
The fill characters and the justification parameters go in as part of the string. For example
In [56]: '{:.<10}'.format('test')
Out[56]: 'test......'
In [57]: '{:.>10}'.format('test')
Out[57]: '......test'
If you want to dynamically generate the format string you could do something like this,
In [76]: '{:{}<{}}'.format('test','.','10')
Out[76]: 'test......'
Yes, if you have a fill, you must have an align. The .
in your test is not treated as a fill, because you are missing an align; instead, the .10
is parsed as the 10
specifying a precision value. See the [.precision]
in the format spec syntax :
format_spec ::= [[fill]align][sign][#][0][width][,][.precision][type]
^^^^^^^^^^^^
For a string, a precision value of 10 will truncate the string to 10 characters:
>>> '{:{}{}}'.format('123456789012345','.','10')
'1234567890'
If you had an align, then the .
would be treated as a fill:
>>> '{:{}>{}}'.format('test','.','10')
'......test'
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.