string formatting - Explain this behaviour with Python's str.format for "general" specifier, left-aligned sign-aware zero-padding. {0:<09.6g} -
can please explain how 'general' format specifier works? i'm confused how left align 0 padding works this. don't know if bug in python or if don't understand documentation saying.
in output below confusion in last 3 blocks of printout, right column. explain, references documented format spec, why they're output that, please?
(this python 3.4.0 (v3.4.0:04f714765c13, mar 16 2014, 19:25:23) [msc v.1600 64 bit (amd64)] on win32
)
test.py:
t = [8123, 0.0, 1.0, 0.799999, 1.3243423487123] v in t: print(str.format("{0:9g} {0:9.6g} {0:09.6g} ", v)) print() v in t: print(str.format("{0:>9g} {0:>9.6g} {0:>09.6g} ", v)) print() v in t: print(str.format("{0:=9g} {0:=9.6g} {0:=09.6g} ", v)) print() # going on right column here??? v in t: print(str.format("{0:<9g} {0:<9.6g} {0:<09.6g} ", v)) print() # going on right column here??? v in t: print(str.format("{0:<9,g} {0:<9,.6g} {0:<09,.6g} ", v)) print() # preceding width field 0 ('0') character enables sign-aware zero-padding numeric types. # equivalent fill character of '0' alignment type of '='. # # why doesn't right column same thing above? v in t: print(str.format("{0:0=9g} {0:0=9.6g} {0:0=09.6g} ", v)) print()
the output of :
$ python3 test.py 8123 8123 000008123 0 0 000000000 1 1 000000001 0.799999 0.799999 00.799999 1.32434 1.32434 001.32434 8123 8123 000008123 0 0 000000000 1 1 000000001 0.799999 0.799999 00.799999 1.32434 1.32434 001.32434 8123 8123 000008123 0 0 000000000 1 1 000000001 0.799999 0.799999 00.799999 1.32434 1.32434 001.32434 8123 8123 812300000 0 0 000000000 1 1 100000000 0.799999 0.799999 0.7999990 1.32434 1.32434 1.3243400 8,123 8,123 8,1230000 0 0 000000000 1 1 100000000 0.799999 0.799999 0.7999990 1.32434 1.32434 1.3243400 000008123 000008123 000008123 000000000 000000000 000000000 000000001 000000001 000000001 00.799999 00.799999 00.799999 001.32434 001.32434 001.32434
the relevant documentation following:
preceding width field 0 (
'0'
) character enables sign-aware zero-padding numeric types. this equivalentfill
character of'0'
alignment type of'='
.
where documentation '='
align option is:
forces padding placed after sign (if any) before digits. used printing fields in form
+000000120
. alignment option valid numeric types.
while <
, >
respectively left , right align given item:
forces field left-aligned within available space (this default objects).
forces field right-aligned within available space (this default numbers).
note use of verb forces. believe here it's stating if <
specified output left-aligned. period. other implied alignments aren't considered (e.g. =
implied 0
filling).
this explains examples provided: if put <
output left-aligned using 09.6g
should 0 padding. did expect <
ignored? believe bad choice because since <
explicit ought honored, while implicit =
should overriden.
i don't puzzles in last example because output is equivalent first output, consistent above documentation.
Comments
Post a Comment