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
๐ŸŒ
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.
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
Trying to format a list of floats with f-strings
You can't format an entire list all at once. You have to format each number individually, and then unpack that to print it: print("Roots:", *(f"{x:.3f}" for x in roots)) or you could use join to combine the formatted numbers: print("Roots:", ' '.join(f"{x:.3f}" for x in roots)) More on reddit.com
๐ŸŒ r/learnpython
6
2
July 23, 2018
How to say this more succinctly in python, and why? ... n = float(format(float(sys.argv[1]), '.2f'))

I'd do this using math rather than string formatting. So if you take any arbitrary number, what series of math operations would you need to remove any trailing digits?

More on reddit.com
๐ŸŒ r/learnprogramming
6
1
October 20, 2021
How to think about integers, strings, and floats?
the difference of integers, strings, and floats integers: whole numbers with no decimal in the value, positive or negative (1, 5000, -4) floats: short for "floating-point number", anything with a decimal point (3.14, 1.0) string: anything text, always surrounded by quotation marks ("word", "3") We've used it for stuff like print(f"You are {age} years old!") Just for clarity's sake, the f there has nothing to do with floats. That's just an F-string for variable interpolation. More on reddit.com
๐ŸŒ r/learnpython
12
1
June 17, 2023
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
๐ŸŒ
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.
๐ŸŒ
Python
docs.python.org โ€บ 3 โ€บ library โ€บ string.html
string โ€” Common string operations
When doing so, float() is used to convert the integer to a floating-point number before formatting.
๐ŸŒ
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.
Find elsewhere
๐ŸŒ
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 ...
๐ŸŒ
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.
๐ŸŒ
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 โ€ฆ
๐ŸŒ
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.
๐ŸŒ
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.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โ€ฆ
๐ŸŒ
Python documentation
docs.python.org โ€บ 3 โ€บ tutorial โ€บ inputoutput.html
7. Input and Output โ€” Python 3.14.3 documentation
The % operator (modulo) can also be used for string formatting. Given format % values (where format is a string), % conversion specifications in format are replaced with zero or more elements of values.
๐ŸŒ
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?
November 13, 2024 - 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)
๐ŸŒ
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.
๐ŸŒ
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
๐ŸŒ
Built In
builtin.com โ€บ data-science โ€บ python-f-string
Guide to String Formatting in Python Using F-strings | Built In
They provide a better way to format strings and make debugging easier, too. ... Summary: Python f-strings, introduced in version 3.6, allow cleaner string formatting by embedding variables directly. They support alignment, zero-padding, decimal precision, percentage display, comma separators and date formatting, making code more readable and debugging easier.
๐ŸŒ
Note.nkmk.me
note.nkmk.me โ€บ home โ€บ python
Format Strings and Numbers in Python: format() | note.nkmk.me
May 18, 2023 - Built-in Functions - format() โ€” Python 3.11.3 documentation ยท This function takes the original string (str) and number (int or float) to be formatted as its first argument, and the format specification string as its second argument.
๐ŸŒ
DZone
dzone.com โ€บ coding โ€บ languages โ€บ python f-strings
Python F-Strings
August 22, 2023 - To format a number using F-strings, simply include the number inside the curly braces, followed by a colon and a format specifier. The format specifier defines how the number should be formatted, including its precision, width, and alignment.
๐ŸŒ
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....