Include the type specifier in your format expression:

>>> a = 10.1234
>>> f'{a:.2f}'
'10.12'
Answer from Robᵩ 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 - To use Python’s format specifiers in a replacement field, you separate them from the expression with a colon (:). 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

Does anyone have a concise cheat sheet for f string formatting with numbers
Here's what I have as a reference. Basically a summary of the Format Specification Mini-Language . This would all go after a : in the f-string curly braces. So if you want a float with 2 decimal place precision, with * as fill and right aligned in a field width of 10 characters you'd do f"{val:*>10.2f}" [[fill]align][sign][#][0][minimumwidth][.precision][type] Fill: Add a character to fill with. Must be followed by Align flag. Align: '<' - Forces the field to be left-aligned within the available space (This is the default.) '>' - Forces the field to be right-aligned within the available space. '=' - Forces the padding to be placed after the sign (if any) but before the digits. This is used for printing fields in the form '+000000120'. This alignment option is only valid for numeric types. '^' - Forces the field to be centered within the available space. Sign: '+' - indicates that a sign should be used for both positive as well as negative numbers '-' - indicates that a sign should be used only for negative numbers (this is the default behavior) ' ' - indicates that a leading space should be used on positive numbers #: Flags alternate numbering formats; binary, octal, and hexadecimal output will be prefixed with '0b', '0o', and '0x', respectively. 0: Zero-padding. Equivalent to fill '=' and character of '0' Minimumwidth: Min width of the field. Precision: Decimal places for floats or max field size for non-numeric types. Type: Integers: 'b' - Binary. Outputs the number in base 2. 'c' - Character. Converts the integer to the corresponding Unicode character before printing. 'd' - Decimal Integer. Outputs the number in base 10. 'o' - Octal format. Outputs the number in base 8. 'x' - Hex format. Outputs the number in base 16, using lower- case letters for the digits above 9. 'X' - Hex format. Outputs the number in base 16, using upper- case letters for the digits above 9. 'n' - Number. This is the same as 'd', except that it uses the current locale setting to insert the appropriate number separator characters. '' (None) - the same as 'd' Floats: 'e' - Exponent notation. Prints the number in scientific notation using the letter 'e' to indicate the exponent. 'E' - Exponent notation. Same as 'e' except it converts the number to uppercase. 'f' - Fixed point. Displays the number as a fixed-point number. 'F' - Fixed point. Same as 'f' except it converts the number to uppercase. 'g' - General format. This prints the number as a fixed-point number, unless the number is too large, in which case it switches to 'e' exponent notation. 'G' - General format. Same as 'g' except switches to 'E' if the number gets to large. 'n' - Number. This is the same as 'g', except that it uses the current locale setting to insert the appropriate number separator characters. '%' - Percentage. Multiplies the number by 100 and displays in fixed ('f') format, followed by a percent sign. '' (None) - similar to 'g', except that it prints at least one digit after the decimal point. More on reddit.com
🌐 r/learnpython
5
3
February 2, 2022
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
19
0
May 19, 2024
python - Rounding floats with f-string - Stack Overflow
Using %-formatting, I can specify the number of decimal cases in a string: ... To be clear, you're only asking about round-to-nearest (like %f does), not round-down/truncate (like int()), round-up, round-towards-zero, round-towards-infinity or any other scheme? If only round-to-nearest, then this is a duplicate (cc: @vaultah) ... But either way, this title "Rounding floats with f-string" and tagging python... More on stackoverflow.com
🌐 stackoverflow.com
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
🌐
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.
Top answer
1 of 2
3
Here's what I have as a reference. Basically a summary of the Format Specification Mini-Language . This would all go after a : in the f-string curly braces. So if you want a float with 2 decimal place precision, with * as fill and right aligned in a field width of 10 characters you'd do f"{val:*>10.2f}" [[fill]align][sign][#][0][minimumwidth][.precision][type] Fill: Add a character to fill with. Must be followed by Align flag. Align: '<' - Forces the field to be left-aligned within the available space (This is the default.) '>' - Forces the field to be right-aligned within the available space. '=' - Forces the padding to be placed after the sign (if any) but before the digits. This is used for printing fields in the form '+000000120'. This alignment option is only valid for numeric types. '^' - Forces the field to be centered within the available space. Sign: '+' - indicates that a sign should be used for both positive as well as negative numbers '-' - indicates that a sign should be used only for negative numbers (this is the default behavior) ' ' - indicates that a leading space should be used on positive numbers #: Flags alternate numbering formats; binary, octal, and hexadecimal output will be prefixed with '0b', '0o', and '0x', respectively. 0: Zero-padding. Equivalent to fill '=' and character of '0' Minimumwidth: Min width of the field. Precision: Decimal places for floats or max field size for non-numeric types. Type: Integers: 'b' - Binary. Outputs the number in base 2. 'c' - Character. Converts the integer to the corresponding Unicode character before printing. 'd' - Decimal Integer. Outputs the number in base 10. 'o' - Octal format. Outputs the number in base 8. 'x' - Hex format. Outputs the number in base 16, using lower- case letters for the digits above 9. 'X' - Hex format. Outputs the number in base 16, using upper- case letters for the digits above 9. 'n' - Number. This is the same as 'd', except that it uses the current locale setting to insert the appropriate number separator characters. '' (None) - the same as 'd' Floats: 'e' - Exponent notation. Prints the number in scientific notation using the letter 'e' to indicate the exponent. 'E' - Exponent notation. Same as 'e' except it converts the number to uppercase. 'f' - Fixed point. Displays the number as a fixed-point number. 'F' - Fixed point. Same as 'f' except it converts the number to uppercase. 'g' - General format. This prints the number as a fixed-point number, unless the number is too large, in which case it switches to 'e' exponent notation. 'G' - General format. Same as 'g' except switches to 'E' if the number gets to large. 'n' - Number. This is the same as 'g', except that it uses the current locale setting to insert the appropriate number separator characters. '%' - Percentage. Multiplies the number by 100 and displays in fixed ('f') format, followed by a percent sign. '' (None) - similar to 'g', except that it prints at least one digit after the decimal point.
2 of 2
1
A cheat sheet for f-string would be too short and kind of useless since f-string are super simple. If you want to put a number just do it. Ex. name: str = "Albus Dumbledore" age: int = 116 print(f"Name: {name}, Age: {age}") As you can see, I simply put age (which is an int) inside 2 brackets in the string (don't forget the f) and let Python do it's magic. (If you want you can use the example above as your cheat sheet)
🌐
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.
🌐
Mimo
mimo.org › glossary › python › formatted-strings
Python Formatted Strings / f-string formatting Guide
Instead of just inserting values, the format mini-language allows you to specify field width, alignment, precision, and more. However, the formatting syntax must follow a specific order, with each modifier category having specific symbols. The format mini-language supports a wide range of options for creating string representations of values. Numbers and dates are the most common values to format: You can control the precision of floating-point numbers, display format, or the grouping of thousands with number formatting.
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 turns 24.125 into “24.125” but 25.0 into “25” ?
🌐
Jerry Ng
jerrynsh.com › 3-useful-python-f-string-tricks-you-probably-dont-know
3 Useful Python F-string Tricks You Probably Don’t Know
August 3, 2021 - When you’re dealing with currency, you would need to prepare your strings to be as user-friendly as possible. For instance, it might be a better idea to format our currency value of 3142671.76 as $3,142,671.76. In such a scenario, formatting our float value as currency using f-string is extremely handy.
🌐
Fanwang Econ
fanwangecon.github.io › Py4Econ › amto › array › htmlpdfr › fs_ary_fstring.html
Python Fstring Numeric Decimal and Significance Formatting
December 14, 2020 - # Define a formatter function def fstring_formater(st_float, it_decimal): # strip the string float, and format with it_decimal number of decimals st_float = st_float.strip() fl_float = float(st_float) st_float_rounded = f'{fl_float:.{it_decimal}f}' return st_float_rounded # Print f'{fstring_formater("1.2345", 3)=}'
🌐
Cjtu
cjtu.github.io › spirl › python_str-formatting.html
3.10. String Formatting (Interactive) — Scientific Programming<br>In Real Life
Python f-strings link · 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 integer format code d · The float format code rounding to a whole number .0f ·
🌐
Bentley
cissandbox.bentley.edu › sandbox › wp-content › uploads › 2022-02-10-Documentation-on-f-strings-Updated.pdf pdf
Updated 2022 A Guide to Formatting with f-strings in Python
Multiplies the number by 100 and displays in fixed ('f') format, ... The variable, variable, is enclosed in curly braces { }. When variable = 10, the f-string · understands that variable is an integer and displays it as such. You can also use specify the · type as n or d and use spacing in ...
🌐
Python
docs.python.org › 3 › library › string.html
string — Common string operations
This limitation doesn’t affect the format() function. The meaning of the various alignment options is as follows: Note that unless a minimum field width is defined, the field width will always be the same size as the data to fill it, so that the alignment option has no meaning in this case. The sign option is only valid for number types, and can be one of the following: The 'z' option coerces negative zero floating-point values to positive zero after rounding to the format precision.
🌐
AskPython
askpython.com › python › built-in-methods › digits-after-decimal-f-string
Fixed digits after decimal with F-string - AskPython
May 31, 2023 - If we write only %f instead of any number, then it will print a float number without rounding behavior. F-string is very faster compared to other formatting techniques available in the Python language.
🌐
DataCamp
datacamp.com › tutorial › python-f-string
Python f-string: A Complete Guide | DataCamp
December 3, 2024 - F-strings are string literals prefixed with 'f' or 'F' that contain expressions inside curly braces {}. These expressions are evaluated at runtime and then formatted using the __format__ protocol. Unlike traditional string formatting methods, f-strings provide a more straightforward and readable way to embed Python expressions directly within string literals.
🌐
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.
🌐
mkaz.blog
mkaz.blog › working-with-python › string-formatting
Python String Formatting: Complete Guide - mkaz.blog
I started this as a quick reference and it has grown into a complete tutorial covering all Python string formatting methods. Use f-strings - Modern, fastest, most readable: f"Hello {name}"
🌐
Fstring
fstring.help
fstring.help: Python f-string guide
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. In our example, this would be 5 characters. ... Some other representations are available too, such as converting the number into an unicode character: ... 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 Morsels
pythonmorsels.com › string-formatting
Python f-string tips & cheat sheets - Python Morsels
April 12, 2022 - Space-padding can be helpful if you're lining up numbers in a fixed-width setting (in a command-line program for example). If you prefer, you can add a space before the Nd specifier (so it'll look more like its sibling, the 0Nd modifier): ... If you'd like to space-pad floating point numbers, check out >N in the section on strings below. The .N% format specifier (where N is a whole number) formats a number as a percentage.