Spies
Spies

Reputation: 19

Using a variable to specify formatting a float using str.format

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

Answers (1)

Kasravnd
Kasravnd

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

Related Questions