numbers = [23.23, 0.1233, 1.0, 4.223, 9887.2]                                                                                                                                                   
                                                                                                                                                                                                
for x in numbers:                                                                                                                                                                               
    print("{:10.4f}".format(x)) 

prints

   23.2300
    0.1233
    1.0000
    4.2230
 9887.2000

The format specifier inside the curly braces follows the Python format string syntax. Specifically, in this case, it consists of the following parts:

  • The empty string before the colon means "take the next provided argument to format()" โ€“ in this case the x as the only argument.
  • The 10.4f part after the colon is the format specification.
  • The f denotes fixed-point notation.
  • The 10 is the total width of the field being printed, lefted-padded by spaces.
  • The 4 is the number of digits after the decimal point.
Answer from Sven Marnach on Stack Overflow
๐ŸŒ
Real Python
realpython.com โ€บ how-to-python-f-string-format-float
How to Format Floats Within F-Strings in Python โ€“ Real Python
March 18, 2026 - In its most basic form, this allows you to define the precision, or number of decimal places, the float will be displayed with. The code below displays the same calculation as before, only itโ€™s displayed more neatly: ... >>> f"One third, rounded to two decimal places is: {1 / 3:.2f}" 'One third, rounded to two decimal places is: 0.33' To use Pythonโ€™s format specifiers in a replacement field, you separate them from the expression with a colon (:).
๐ŸŒ
Python.org
discuss.python.org โ€บ ideas
New format specifiers for string formatting of floats with SI and IEC prefixes - Ideas - Discussions on Python.org
May 19, 2023 - I was thinking of writing a PEP to support formatting floats with SI (decimal) and IEC (binary) prefixes natively for float (and maybe other types if makes sense) but wanted to feel it out first. Iโ€™ve implemented what I want to propose in the Prefixed package as a subclass of float.
Discussions

Do you normally use string.format() or percentage (%) to format your Python strings?

Firstly, you can write e. g. {0:.2f} to specify a float with 2 decimals, see e. g. https://www.digitalocean.com/community/tutorials/how-to-use-string-formatters-in-python-3

Secondly, the best formatting method is f-strings, see e. g. https://www.blog.pythonlibrary.org/2018/03/13/python-3-an-intro-to-f-strings/

More on reddit.com
๐ŸŒ r/Python
130
75
June 3, 2018
How to use the float() command to create 2 decimal places instead of one in a product of 2 numbers [the product is dollar amount]
You generally don't round until you go to display the value. You can use round for that, but since you just want to display it with two digits, you can use string formatting . In your case, something like print(f'{totalPay:.2f}'). .2f is the format specifier, saying round the float (f) to two decimal places (.2). More on reddit.com
๐ŸŒ r/learnpython
9
7
January 28, 2020
People also ask

Does .2f change the original float?
No. Formatting returns text, and the original float stays unchanged unless your code assigns a different value.
๐ŸŒ
askpython.com
askpython.com โ€บ python โ€บ string โ€บ python-floating-point-formatting
Python .2f: Format Floats to Two Decimal Places - AskPython
What does .2f mean in Python?
The .2f format specifier displays a number in fixed-point notation with two digits after the decimal point. Its result is a string.
๐ŸŒ
askpython.com
askpython.com โ€บ python โ€บ string โ€บ python-floating-point-formatting
Python .2f: Format Floats to Two Decimal Places - AskPython
Does .2f round or truncate?
It rounds the displayed value to the requested precision rather than cutting off later digits. The result still depends on the value Python stores.
๐ŸŒ
askpython.com
askpython.com โ€บ python โ€บ string โ€บ python-floating-point-formatting
Python .2f: Format Floats to Two Decimal Places - AskPython
๐ŸŒ
Medium
medium.com โ€บ @coucoucamille โ€บ float-formatting-in-python-ccb023b86417
Simple Float Formatting in Python | by Coucou Camille | Medium
June 15, 2022 - Pythonโ€™s built-in format() function allows you to format float in any way you prefer. Syntax: {:.2f}.format(num) for rounding to 2 decimal places. {} marks a replacement field ยท : introduces a format specifier ยท .2 specify the precision as 2, or any other number ยท
๐ŸŒ
AskPython
askpython.com โ€บ python โ€บ string โ€บ python-floating-point-formatting
Python .2f: Format Floats to Two Decimal Places - AskPython
1 week ago - In Python, .2f formats a number as fixed-point text with two digits after the decimal point. The colon starts the format specification, 2 sets the precision, and f selects fixed-point presentation.
๐ŸŒ
GeeksforGeeks
geeksforgeeks.org โ€บ python โ€บ string-formatting-in-python
String Formatting in Python - GeeksforGeeks
The %5.4f format specifier is used to format a floating-point number with a minimum width of 5 and a precision of 4 decimal places. ... In the given code, the formatting string Python is converted to Integer and floating point with %d,%f.
Published: March 18, 2026
Find elsewhere
๐ŸŒ
CodeRivers
coderivers.org โ€บ blog โ€บ python-format-specifier
Python Format Specifier: A Comprehensive Guide - CodeRivers
February 22, 2026 - For example, f - strings were introduced in Python 3.6. If you need to support older versions, use the format method instead. When using format specifiers, make sure to handle potential errors. For example, if you expect a certain data type in a format specifier but receive a different type, it can lead to errors. try: value = "not a number" formatted_value = "{:.2f}".format(float(value)) except ValueError as e: print(f"Error: {e}")
๐ŸŒ
PW Skills
pwskills.com โ€บ blog โ€บ 2f-in-python-what-does-it-mean
What Does %.2f Mean in Python?
In Python, โ€œ2fโ€ refers to the formatting directive used to display a floating-point number with two digits after the decimal. It is usually written as part of a larger format specifier like โ€œ%.2fโ€ or โ€œ{:.2f}โ€, where the โ€œ2fโ€ ...
๐ŸŒ
Python
peps.python.org โ€บ pep-3101
PEP 3101 โ€“ Advanced String Formatting | peps.python.org
So for example, in the case where the โ€˜intโ€™ formatter sees a format type of โ€˜fโ€™ (meaning โ€˜floatโ€™) it can simply cast the value to a float and call format() again. Any class can override the __format__ method to provide custom formatting ...
๐ŸŒ
Appdividend
appdividend.com โ€บ how-to-format-float-values-in-python
How to Format Float Values in Python
December 13, 2025 - To format float values in Python, use the format() method or f-strings or just format the value using %.2f inside the print() method.
๐ŸŒ
Python Reference
python-reference.readthedocs.io โ€บ en โ€บ latest โ€บ docs โ€บ functions โ€บ format.html
format โ€” Python Reference (The Right Way) 0.1 documentation
If not specified, then the field width will be determined by the content. Preceding the width field by a zero (โ€˜0โ€™) character enables sign-aware zero-padding for numeric types. This is equivalent to a fill character of โ€˜0โ€™ with an alignment type of โ€˜=โ€™. The precision is a decimal number indicating how many digits should be displayed after the decimal point for a floating point value formatted with โ€˜fโ€™ and โ€˜Fโ€™, or before and after the decimal point for a floating point value formatted with โ€˜gโ€™ or โ€˜Gโ€™. For non-number types the field indicates the maximum field size - in other words, how many characters will be used from the field content.
๐ŸŒ
Finxter
blog.finxter.com โ€บ python-format-function
Python format() Function: No-BS Guide by Example โ€“ Be on the Right Side of Change
February 5, 2021 - The 'f' format specifier simply stands for the formatting objective 'float'.
๐ŸŒ
freeCodeCamp
freecodecamp.org โ€บ news โ€บ 2f-in-python-what-does-it-mean
%.2f in Python โ€“ What does it Mean?
June 22, 2022 - 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 fl...
๐ŸŒ
py4u
py4u.org โ€บ blog โ€บ python-precision-in-string-formatting-with-float-numbers
Python Float String Formatting: How to Preserve Full Precision Without Manual Decimal Specification
For 64-bit floats (the default in Python), it returns up to 17 significant digitsโ€”the minimum needed to uniquely identify the float. Use case: Debugging, logging, or any scenario where you need to reconstruct the exact float later. Caveat: repr() output can be verbose (e.g., repr(1000000.0) โ†’ '1000000.0' instead of '1000000'). The 'g' (general) format specifier removes insignificant trailing zeros and uses exponential notation for small/large numbers.
๐ŸŒ
LabEx
labex.io โ€บ questions โ€บ how-to-use-format-specifiers-in-python-91
How to Use Format Specifiers in Python | LabEx
July 25, 2024 - Some of the most common ones include: ... Numeric Format Specifiers: Integer: {:d} or {:n} Floating-Point: {:f}, {:.nf} (where n is the number of decimal places) Percentage: {:.n%} (where n is the number of decimal places) String ...
๐ŸŒ
LabEx
labex.io โ€บ tutorials โ€บ python-how-to-format-the-output-of-floating-point-numbers-in-python-397681
How to format the output of floating-point numbers in Python | LabEx
These techniques can be particularly useful when dealing with large or small numbers, or when you need to control the overall appearance of the output. To display a floating-point number in scientific notation, you can use the e or E format ...
๐ŸŒ
PyFormat
pyformat.info
PyFormat: Using % and .format() for great good!
The number behind a . in the format specifies the precision of the output. For strings that means that the output is truncated to the specified length.
๐ŸŒ
AskPython
askpython.com โ€บ python โ€บ string โ€บ print-format-3f
Understanding the Print Format %.3f in Python - AskPython
March 30, 2023 - In Python, the โ€œ%.3fโ€ format specifier is a method for formatting floating-point numbers to display only three decimal places. This is achieved using the โ€˜%โ€™ operator along with the โ€˜%.nfโ€™ format specifier, where โ€˜nโ€™ determines the number of decimal places.
๐ŸŒ
University of Southampton
southampton.ac.uk โ€บ ~fangohr โ€บ teaching โ€บ python โ€บ book โ€บ html โ€บ 05-input-output.html
05-input-output
The format specifier of type %W.Df means that a Float should be printed with a total Width of W characters and D digits behind the Decimal point.
๐ŸŒ
Codesolid
codesolid.com โ€บ python-format-strings
Python Format Strings: Beginner to Expert โ€” CodeSolid.com 0.1 documentation
These format specifiers wonโ€™t be new to you if youโ€™ve worked in other languages. They form the basis of string formatting methods in different languages, dating back to C in the 1980s. For example, F-strings make it easy to format floating-point numbers by allowing you to specify the number of decimal places. Youโ€™re a teacher, letโ€™s say, and you decide that the default values Python ...