Jan-24-2018, 07:00 PM
(Jan-24-2018, 06:54 PM)j.crater Wrote: Is it possible to have width (in this case 4 and 40) as a variable, so it can vary depending on text length?Here is an eight years old example of variable width and precision
rabbits = { "flopsy" : 1.0/3, "mopsy" : 576.0/7, "cotton tail": .76/5, "peter": 300000.0/37, } nwidth = 1 + max(len(name) for name in rabbits) for name in sorted(rabbits): # the floating point precision is passed as argument to format print("{name:{namewidth}}:{score:>10.{precision}f}".format( name = name, score = rabbits[name], namewidth = nwidth, precision = 2)) """my output ----> cotton tail : 0.15 flopsy : 0.33 mopsy : 82.29 peter : 8108.11 """