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
April 24, 2024 - As you can see, your float has been rounded to two decimal places. You achieved this by adding the format specifier .2f into the replacement field. The 2 is the precision, while the lowercase f is an example of a presentation type. You’ll see more of these later. Note: When you use a format specifier, you don’t actually change the underlying number. You only improve its display. Python’s f-strings also have their own mini-language that allows you to format your output in a variety of different ways.
Discussions

General way to print floats without the .0 part
I’m building SVG code using data interpolation (f-strings and .format), and I have elements (the size of the graph for one) that are internally floats but which are usually integers. But when printing floats, the .0 part is always included. Is there a standard str-interpolation idiom that ... More on discuss.python.org
🌐 discuss.python.org
0
0
May 19, 2024
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
July 7, 2016
HELP - Have a pandas dataframe of floats but want to format the # of decimal places

Don't change the dataframe. Change the presentation of the values when you print or display the data. There's no reason to truncate floats in your actual data, and you can't pad a float with zeros, you can only pad a string.

More on reddit.com
🌐 r/learnpython
2
1
January 1, 2018
How to eliminate trailing zeros?
Since you're already formatting the number as a string, you could just check to see if the last value is a '0', then remove it. Not an efficient solution, but something readable so you have an idea: number = 25.159 num_string = format(number, ".2f") if num_string[-1] == '0': formatted_num = num_string[:-1] else: formatted_num = num_string print(formatted_num) More on reddit.com
🌐 r/learnpython
13
3
October 3, 2019
People also ask

What does "%.2f" mean in Python?
“%.2f” tells Python to format a floating-point number with exactly 2 digits after the decimal. It rounds the value if needed and outputs a clean, consistent numeric format. This is commonly used in reports, billing systems, financial calculations, and formatted print statements.
🌐
pwskills.com
pwskills.com › blog › python › what does %.2f mean in python?
What Does %.2f Mean In Python? Format Float To 2 Decimal Places ...
Are there alternatives to %.2f in Python?
Yes. Alternatives include f-strings (f"{value:.2f}"), format() (“{:.2f}”.format(value)), and the round() function for non-formatting purposes.
🌐
pwskills.com
pwskills.com › blog › python › what does %.2f mean in python?
What Does %.2f Mean In Python? Format Float To 2 Decimal Places ...
What does .2f do in Python?
“.2f” specifies the precision part of a format string, ensuring the float shows only two decimal places. It applies rounding automatically and works with print(), f-strings, and format().
🌐
pwskills.com
pwskills.com › blog › python › what does %.2f mean in python?
What Does %.2f Mean In Python? Format Float To 2 Decimal Places ...
🌐
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.
🌐
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 ...
🌐
PyFormat
pyformat.info
PyFormat: Using % and .format() for great good!
Of course it is also possible to format numbers. ... Similar to strings numbers can also be constrained to a specific width. ... Again similar to truncating strings the precision for floating point numbers limits the number of positions after the decimal point.
🌐
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.
Find elsewhere
🌐
PW Skills
pwskills.com › blog › python › what does %.2f mean in python?
What Does %.2f Mean In Python? Format Float To 2 Decimal Places Explained
October 30, 2025 - In this example, value = 123.456789 assigns the value 123.456789 to the variable value. This is a floating-point number. ... {0:.2f} is a format specifier within a string. It’s used with the .format() method or f-strings for string formatting in Python.
🌐
Python documentation
docs.python.org › 3 › tutorial › floatingpoint.html
15. Floating-Point Arithmetic: Issues and Limitations — Python 3.14.3 documentation
While pathological cases do exist, for most casual use of floating-point arithmetic you’ll see the result you expect in the end if you simply round the display of your final results to the number of decimal digits you expect. str() usually suffices, and for finer control see the str.format() method’s format specifiers in Format String Syntax.
🌐
AskPython
askpython.com › home › understanding the print format %.3f in python
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.
🌐
Appdividend
appdividend.com › 2022 › 06 › 23 › 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.
🌐
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   January 14, 2026
🌐
Programiz
programiz.com › python-programming › methods › built-in › format
Python format()
The sign option in formatting controls the display of the sign for the numbers. Here, + indicates that both positive and negative numbers will show their signs. Note: The sign option - indicates only negative numbers will show their sign and ' ' will show a space for positive numbers and a minus for negative numbers. Precision allows us to define the number of digits to be shown after a decimal point. For example, # set precision to 2 for floating-point number precision_value = format(123.4567, '.2f') print(precision_value) # Output: 123.46
🌐
Python.org
discuss.python.org › python help
General way to print floats without the .0 part - Python Help - Discussions on Python.org
May 19, 2024 - I’m building SVG code using data interpolation (f-strings and .format), and I have elements (the size of the graph for one) that are internally floats but which are usually integers. But when printing floats, the .0 part is always included. Is there a standard str-interpolation idiom that ...
🌐
AskPython
askpython.com › home › python floating point formatting: 2 simple methods
Python Floating Point Formatting: 2 Simple Methods - AskPython
April 10, 2025 - In this example, the :.2f inside the curly braces states that the floating-point number should be formatted with two decimal places. Running this code would output 123.46. ... Here, :10.2f states that the total width of the formatted string should be 10 characters, including two decimal places. The output, in this case, would be 123.46 · Python’s format method offers a versatile approach to string formatting. Here we can provide complex specifications to format the string, which makes it useful in different real-world applications.
🌐
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 for that type: ... Note for Python 2.x: The ‘format_spec’ argument will be either a string object or a unicode object, depending on the type of the original format string. The __format__ method should test the type of the specifiers parameter to determine whether to return a string or unicode object.
🌐
Python Engineer
python-engineer.com › posts › limit-float-python
How to limit float values to N decimal places in Python - Python Engineer
June 20, 2022 - The format specifier we need here is .Nf where N is the number of decimal places expected in output.
🌐
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...
🌐
Analytics Vidhya
analyticsvidhya.com › home › 6 ways to round floating value to two decimals in python
6 Ways to Round Floating Value to Two Decimals in Python
November 4, 2024 - To format a floating-point number with two decimal places in Python, you can use the .2f format specifier.
🌐
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 ...
🌐
ZetCode
zetcode.com › python › fstring
Python f-string - formatting strings in Python with f-string
May 11, 2025 - Here, the format specifier is stored in a variable and then used inside the f-string. This technique is useful for creating flexible and reusable formatting code. $ python main.py 16.03.24 24/03/16 · F-strings make it easy to control the number of decimal places when displaying floating point numbers.