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
May 19, 2024
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
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
Pandas ignores dtype string and reads percentage as float value

It's possible that the actual value is 0.05 but that Excel displays 5% to you because of formatting settings of the cell.

How to get around that I don't know, but it could be an explanation.

More on reddit.com
๐ŸŒ r/Python
3
1
October 17, 2016
๐ŸŒ
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.
๐ŸŒ
GeeksforGeeks
geeksforgeeks.org โ€บ python โ€บ input-and-output-in-python
Input and Output in Python - GeeksforGeeks
The code prompts the user to input a string (the color of a rose), assigns it to the variable color and then prints the inputted color. ... The code prompts the user to input an integer representing the number of roses, converts the input to an integer using typecasting and then prints the integer value. ... The code prompts the user to input the price of each rose as a floating-point number, converts the input to a float using typecasting and then prints the price.
Published ย  1 week ago
๐ŸŒ
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.
Find elsewhere
๐ŸŒ
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.
๐ŸŒ
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
๐ŸŒ
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 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.
๐ŸŒ
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.
๐ŸŒ
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.
๐ŸŒ
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โ€ฆ
๐ŸŒ
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....