Python String Formatting, difference in code -
hi trying understand how string formatting works float:
i have tried
>>> print("%s %s %s %s %-9.10f"%("this","is","monday","morning",56.3648)) it gives output of
this monday morning 56.3648000000 however this,
>>> print("%s %s %s %s %10f"%("this","is","monday","morning",56.3648)) gives output of
this monday morning 56.364800 what causing difference?
the way pattern strings parsed. %9.10f sets (minimum) field width 9 , precision 10, while %10f sets width 10. think meant write %.10f instead:
in [4]: '%10f' % 56.3648 # width out[4]: ' 56.364800' in [5]: '%.10f' % 56.3648 # precision out[5]: '56.3648000000' also, consider using the newer str.format formatting style. first example turn into
in [6]: '{} {} {} {} {:<9.10f}'.format('this', 'is', 'monday', 'morning', 56.3648) out[6]: 'this monday morning 56.3648000000'