Reputation: 19
I would like to use a variable to specify the number of decimals to format as in this case: (python 3.4.6)
print(str.format('{0:.intVariableName}',1.2345678))
Upvotes: 1
Views: 29
Reputation: 107287
You can use variable name as another argument in format
function:
In [29]: print(str.format('{0:.{var}}',1.2345678, var=3))
1.23
Upvotes: 1