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.
People also ask

How to format floating numbers using inbuilt methods in Python?
We can use the 'format' method to specify the width and assignment of the floating numbers.
๐ŸŒ
askpython.com
askpython.com โ€บ home โ€บ python floating point formatting: 2 simple methods
Python Floating Point Formatting: 2 Simple Methods - AskPython
How do I limit a float to only show 2 decimal places?
You can limit a float to 2 decimal places using either f-strings or the format() method. For f-strings, use f"{value:.2f}" and for format(), use "{:.2f}".format(value).
๐ŸŒ
askpython.com
askpython.com โ€บ home โ€บ python floating point formatting: 2 simple methods
Python Floating Point Formatting: 2 Simple Methods - AskPython
How do I display infinity or nan values properly?
Use the .2g format to display inf, -inf, and nan in a consistent readable format like you would expect in a calculator.
๐ŸŒ
askpython.com
askpython.com โ€บ home โ€บ python floating point formatting: 2 simple methods
Python Floating Point Formatting: 2 Simple Methods - AskPython
๐ŸŒ
Medium
medium.com โ€บ @coucoucamille โ€บ float-formatting-in-python-ccb023b86417
Simple Float Formatting in Python | by Coucou Camille | Medium
June 15, 2022 - Simple Float Formatting in Python Pythonโ€™s built-in format() function allows you to format float in any way you prefer. 1. Round Float to 2 Decimal Places Syntax: {:.2f}.format(num) for rounding to โ€ฆ
๐ŸŒ
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.
๐ŸŒ
Python Reference
python-reference.readthedocs.io โ€บ en โ€บ latest โ€บ docs โ€บ functions โ€บ format.html
format โ€” Python Reference (The Right Way) 0.1 documentation
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. The precision is not allowed for integer values. Determines how the data should be presented. The available string presentation types are: โ€˜sโ€™
๐ŸŒ
Cjtu
cjtu.github.io โ€บ spirl โ€บ python_str-formatting.html
3.10. String Formatting (Interactive) โ€” Scientific Programming In Real Life
To print the following lines as 10 characters each, we would specify .f as the format code and Python will automatically add spaced in front to make the output 10 characters long: print(".f" % 1) print(".f" % 10) print(".f" % 100) print(".f" % 1000) print(".f" % 10000) ... Below we use the % operator after the string to include the three elements into the three locations denoted by % within the string. We use three different format codes for the three numbers included in the string: ... The float format code rounding to 2 decimal places .2f.
๐ŸŒ
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.
Find elsewhere
๐ŸŒ
mkaz.blog
mkaz.blog โ€บ working-with-python โ€บ string-formatting
Python String Formatting: Complete Guide
This comprehensive guide covers everything you need to know about Python string formatting, from basic f-strings to advanced formatting techniques.
๐ŸŒ
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 ...
๐ŸŒ
Python Course
python-course.eu โ€บ python-tutorial โ€บ formatted-output.php
22. Formatted Output | Python Tutorial | python-course.eu
This is followed by the total number of digits the string should contain. This number includes the decimal point and all the digits, i.e. before and after the decimal point. Our float number 59.058 has to be formatted with 8 characters. The decimal part of the number or the precision is set to 2, i.e.
๐ŸŒ
W3Schools
w3schools.com โ€บ python โ€บ python_string_formatting.asp
Python String Formatting
To format values in an f-string, add placeholders {}, a placeholder can contain variables, operations, functions, and modifiers to format the value. ... A placeholder can also include a modifier to format the value.
๐ŸŒ
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 - However, modern formatting methods ... format a floating-point number with two decimal places in Python, you can use the .2f format specifier....
๐ŸŒ
TutorialsPoint
tutorialspoint.com โ€บ how-to-format-a-floating-number-to-fixed-width-in-python
How to format a floating number to fixed width in Python?
The str.format() method is versatile in formatting strings in Python. By using this method we can also control padding and alignment. number = 123.4568 formatted = "{:10.2f}".format(number) print(formatted)
๐ŸŒ
Appdividend
appdividend.com โ€บ how-to-format-float-values-in-python
How to Format Float Values in Python
December 13, 2025 - value = 123.45678 formatted_value = "{:.1f}".format(value) print(float(formatted_value)) # Output: 123.5 ยท The % operator formats the strings oldway.
๐ŸŒ
Python
docs.python.org โ€บ 3 โ€บ library โ€บ string.html
Common string operations โ€” Python 3.14.3 documentation
When doing so, float() is used to convert the integer to a floating-point number before formatting.
๐ŸŒ
Finxter
blog.finxter.com โ€บ python-convert-float-to-string
Python Convert Float to String โ€“ Be on the Right Side of Change
The most Pythonic way to convert a float to a string is to pass the float into the built-in str() function. For example, str(1.23) converts the float 1.23 to the string '1.23'. ... To set the decimal precision after the comma, you can use the f-string formatting functionality in Python 3.
๐ŸŒ
Python.org
discuss.python.org โ€บ ideas
New format specifiers for string formatting of floats with SI and IEC prefixes - Page 2 - Ideas - Discussions on Python.org
May 20, 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โ€ฆ
๐ŸŒ
Mooc
programming-24.mooc.fi โ€บ part-4 โ€บ 5-print-statement-formatting
Print statement formatting - Python Programming MOOC 2024
The format specifier .2f states that we want to display 2 decimals. The letter f at the end means that we want the variable to be displayed as a float, i.e. a floating point number. Here's another example, where we specify the amount of whitespace reserved for the variable in the printout. Both times the variable name is included in the resulting string, it has a space of 15 characters reserved.
๐ŸŒ
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 ย  August 17, 2020