Python - decimal places (putting floats into a string)

前端 未结 3 1642
广开言路
广开言路 2021-01-18 02:17

I have been using the format:

print \'blah, blah %f\' %variable

to put variables into strings. I heard it was more pythonic than the \'+str

3条回答
  •  萌比男神i
    2021-01-18 03:17

    For Python version 3.6 and upwards there is also the option to use f-strings, which works in a quite similar fashion to what Kirk Strauser showed in his answer

    print(f'blah, blah {variable:.2f}')
    

提交回复
热议问题