What does .2f mean in python?

In Python, there are various methods for formatting data types.

The %f formatter is specifically used for formatting float values (numbers with decimals).

We can use the %f formatter to specify the number of decimal numbers to be returned when a floating point number is rounded up.

How to Use the %f Formatter in Python

In this section, you'll see some examples on how to use the %f formatter and the various parameters it can be used with to return varying results.

Here's the first example:

floatNumber = 1.9876

print("%f" % floatNumber)
# 1.987600

Using %f in the example above added two zeros to the number. But there's nothing too special about that. We'll see what else we can do to modify the resulting value soon.

Note that the %f formatter must be nested inside quotation marks, and should be separated from the floating number which it is formatting by a modulo operator (%): "%f" % floatNumber.

Let's take a look at another example.

floatNumber = 1.9876

print("%.1f" % floatNumber)
# 2.0

In the code above, we added .1 between % and f in the %f operator. This means that we want the number to be rounded up to one decimal place.

Note that you'll get a similar result to the one in the first example if you omit the period/dot symbol (.) that comes before the digit we passed in-between % and f.

The resulting value in our example is 2.0 which was returned when 1.9876 was rounded up to one decimal place.

Let's use %.2f and see what happens.

floatNumber = 1.9876

print("%.2f" % floatNumber)
# 1.99

As expected, the floating point number (1.9876) was rounded up to two decimal places – 1.99. So %.2f means to round up to two decimal places.

You can play around with the code to see what happens as you change the number in the formatter.

How to Use the %d Formatter in Python

Another formatting method we can use with floating point numbers in Python is the %d formatter. This returns the whole number in a floating point number.

Here's an example:

floatNumber = 1.9876

print("%d" % floatNumber)
# 1

In the example above, we created a floating point number: floatNumber = 1.9876.

When the floatNumber variable was formatted using %d, only 1 was returned.

The %d formatter ignores the decimal numbers and returns only the whole number.

Summary

In this article, we talked about the %f formatter in Python. You use it to format floating point numbers.

Depending on the parameters provided, the %f formatter rounds up a float value to the nearest decimal place provided.

We also talked about the %d formatter which returns only a whole number from a floating point number.

Happy coding!

Learn to code for free. freeCodeCamp's open source curriculum has helped more than 40,000 people get jobs as developers. Get started

  1. Home
  2. Blog
  3. Python String Formatting

(Sponsors) Get started learning Python with DataCamp's free Intro to Python tutorial. Learn Data Science by completing interactive coding challenges and watching videos by expert instructors. Start Now!


Updated on Jan 07, 2020


The format() method allows you format string in any way you want.

Syntax: template.format(p1, p1, .... , k1=v1, k2=v2)

template is a string containing format codes, format() method uses it's argument to substitute value for each format codes. For e.g:

>>> 'Sam has {0} red balls and {1} yellow balls'.format(12, 31)

{0} and {1} are format codes. The format code {0} is replaced by the first argument of format() i.e 12, while {1} is replaced by the second argument of format() i.e 31.

Expected Output:

Sam has 12 red balls and 31 yellow balls

This technique is okay for simple formatting but what if you want to specify precision in floating point number? For such thing you need to learn more about format codes. Here is the full syntax of format codes.

Syntax: {[argument_index_or_keyword]:[width][.precision][type]}

The type can be used with format codes:

Format codesDescription
d for integers
f for floating point numbers
b for binary numbers
o for octal numbers
x for octal hexadecimal numbers
s for string
e for floating point in exponent format

Following examples will make things more clear.

Example 1:

>>> "Floating point {0:.2f}".format(345.7916732)

Here we specify 2 digits of precision and f is used to represent floating point number.

Expected Output:

Example 2:

>>> import math
>>> "Floating point {0:10.3f}".format(math.pi)

Here we specify 3 digits of precision, 10 for width and f for floating point number.

Expected Output:

Example 3:

"Floating point pi = {0:.3f}, with {1:d} digit precision".format(math.pi, 3)

Here d in {1:d} represents integer value.

Expected Output:

Floating point pi = 3.142, with 3 digit precision

You need to specify precision only in case of floating point numbers if you specify precision for integer ValueError will be raised.

Example 5:

'Sam has {1:d} red balls and {0:d} yellow balls'.format(12, 31)

Expected Output:

Sam has 31 red balls and 12 yellow balls

Example 6:

"In binary 4 is {0:b}".format(4) # b for binary, refer to Fig 1.1

Expected Output:

Example 7:

array = [34, 66, 12]
"A = {0}, B = {1}, C = {2}".format(*array)

Expected Output:

Example 8:

d = {
'hats' : 122,
'mats' : 42
}

"Sam had {hats} hats and {mats} mats".format(**d)

Expected Output:

Sam had 122 hats and 42 mats

The format() method also supports keywords arguments.

'Sam has {red} red balls and {green} yellow balls'.format(red = 12, green = 31)

Note while using keyword arguments we need to use arguments inside {} not numeric index.

You can also mix position arguments with keywords arguments

'Sam has {red} red balls, {green} yellow balls \
and {0} bats'.format(3, red = 12, green = 31)

The format() method of formatting string is quite new and was introduced in Python 2.6 . There is another old technique  you will see in legacy codes which allows you to format string using % operator instead of format() method.

Let's take an example.

"%d pens cost = %.2f" % (12, 150.87612)

Here we are using template string on the left of %. Instead of {} for format codes we are using %. On the right side of % we use tuple to contain our values. %d and %.2f are called as format specifiers, they begin with % followed by character that represents the data type. For e.g %d format specifier is a placeholder for a integer, similarly %.2f is a placeholder for floating point number.

So %d is replaced by the first value of the tuple i.e 12 and %.2f is replaced by second value i.e 150.87612.

Expected Output:

Some more examples.

Example 1:

New:

"{0:d} {1:d} ".format(12, 31)

Old:

Expected Output:

Example 2:

New:

"{0:.2f} {1:.3f}".format(12.3152, 89.65431)

Old:

"%.2f %.3f" % (12.3152, 89.65431)

Expected Output:

Example 3:

New:

"{0:s} {1:o} {2:.2f} {3:d}".format("Hello", 71, 45836.12589, 45 )

Old:

"%s %o %.2f %d" % ("Hello", 71, 45836.12589, 45 )

Expected Output:


Other Tutorials (Sponsors)

This site generously supported by DataCamp. DataCamp offers online interactive Python Tutorials for Data Science. Join over a million other learners and get started learning Python for data science today!


What is 2f area in Python?

As expected, the floating point number (1.9876) was rounded up to two decimal places – 1.99. So %. 2f means to round up to two decimal places. You can play around with the code to see what happens as you change the number in the formatter.

What is %2f?

9 votes. “print” treats the % as a special character you need to add, so it can know, that when you type “f”, the number (result) that will be printed will be a floating point type, and the “. 2” tells your “print” to print only the first 2 digits after the point.

What does .0f mean in Python?

Number Formatting.

What is %d and %s in Python?

%s is used as a placeholder for string values you want to inject into a formatted string. %d is used as a placeholder for numeric or decimal values. For example (for python 3) print ('%s is %d years old' % ('Joe', 42))