Python - decimal places (putting floats into a string)

24,263

Solution 1

>>> variable = 12
>>> print 'blah, blah %4.3f' %variable
blah, blah 12.000
>>> print 'blah, blah %1.1f' %variable
blah, blah 12.0

Here is the Python Doc Link, please consider:

Since str.format() is quite new, a lot of Python code still uses the % operator. However, because this old style of formatting will eventually be removed from the language, str.format() should generally be used.

Solution 2

In Python version 2.6 and newer, you can use:

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

where "0" refers to the first value passed into str.format, ":" says "here comes the format specification", and ".2f" means "floating point number with two decimal places of precision". This is the suggested way of formatting strings now.

Solution 3

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}')
Share:
24,263
Anake
Author by

Anake

Updated on July 18, 2022

Comments

  • Anake
    Anake almost 2 years

    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()+' approach, and have got quite used to it. Is there a way of specifying the decimal places added to the string with %f? I have tried rounding the number before supplying the variable.

    Thanks

  • Chris Morgan
    Chris Morgan almost 13 years
    The switch from % to str.format is something I've never understood. Both work fine, but the printf-style one is something people will already be familiar with, and it is distinctly faster, and it uses less code. I continue to use % most of the time for myself, and as it's still around in Python 3.2 I think I'll continue to do it even when using Python 3 for lots of things.
  • Anake
    Anake almost 13 years
    hope they do keep it in, it's definitely quicker to code and less annoying!
  • Kirk Strauser
    Kirk Strauser almost 13 years
    @Chris Morgan, see PEP 3101. One of the things I like a lot about str.format is that it's so easy to switch between positional and named arguments without having to re-write the template.
  • Chris Morgan
    Chris Morgan almost 13 years
    @Kirk: I have read it before but it didn't help me to see why they would recommend not using %, or why it would eventually be removed.
  • Kirk Strauser
    Kirk Strauser almost 13 years
    @Chris: Well, the biggest reason is that % only accepts one argument, so you have to choose wisely. If you pick wrong (say you were passing in a tuple of positional values and want to add a keyword value), you have to re-write the whole thing. With format, you can pass in any combination of arguments that any other function would accept. I'm sure someone will also come up with a use case where having format as a first-class function is handy.
  • Chris Morgan
    Chris Morgan almost 13 years
    @Kirk: but % is so much easier/shorter to use! (And it's ugly in what I think is the most common usage of %, just joining multiple parts where all quantities are known.)
  • Kirk Strauser
    Kirk Strauser almost 13 years
    @Chris: Don't get me wrong: I use % all the time. You'll find it a lot more often than format in my own code. Still, I can see a few advantages in format and I'll probably start working it into my projects more often.
  • Little Bobby Tables
    Little Bobby Tables over 7 years
    What does the 4 do in print 'blah, blah %4.3f' %variable do?
  • Kirk Strauser
    Kirk Strauser over 3 years
    The only reason I use my answer these days is when I need to define a template somewhere then fill it in later. I use f-strings for pretty much everything else. They're lovely!
  • Johann
    Johann over 3 years
    Yeah it's the same for me. That's why I figured f-strings would be useful to add to this question