How about this

x = 3.14159265
print(f'pi = {x:.2f}')

Docs for f-strings

Answer from JBernardo 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 - 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.
Discussions

python - Fixed digits after decimal with f-strings - Stack Overflow
Is there an easy way with Python f-strings to fix the number of digits after the decimal point? (Specifically f-strings, not other string formatting options like .format or %) For example, let's s... More on stackoverflow.com
🌐 stackoverflow.com
Pythonic way to show 2 decimal places in f-strings?

Do the second one, just don’t worry about rounding it. Let the formatting do the rounding for display.

More on reddit.com
🌐 r/learnpython
10
1
March 1, 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
python - Displaying floats using F-string - Stack Overflow
I'm really curious about why the behaviour is so different when add this "f" to the end of the number which I want to display: # CASE with f float1 = 10.1444786 print(f"{float1:.4f}... More on stackoverflow.com
🌐 stackoverflow.com
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 › python › string › python-floating-point-formatting
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 › python › string › python-floating-point-formatting
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 › python › string › python-floating-point-formatting
Python Floating Point Formatting: 2 Simple Methods - AskPython
🌐
ZetCode
zetcode.com › python › fstring
Python f-string - formatting strings in Python with f-string
May 11, 2025 - F-strings make it easy to control the number of decimal places when displaying floating point numbers. You can specify the precision directly after the colon, ensuring your output is as precise or as concise as you need.
🌐
Built In
builtin.com › data-science › python-f-string
Guide to String Formatting in Python Using F-strings | Built In
For the following sections, we’ll be using the syntax below: Syntax: {:[width][.precision][type]} Type: d: integers f: floating point numbers *You need to specify precision only in case of floating point numbers
🌐
AskPython
askpython.com › python › built-in-methods › digits-after-decimal-f-string
Fixed digits after decimal with F-string - AskPython
May 31, 2023 - The f-string method is used for formatting the strings and helps to fix the number of digits after the decimal point. The precision is handled by the F-string method. The %f format is used for rounding float numbers after decimal points.
🌐
Miguendes
miguendes.me › 73-examples-to-help-you-master-pythons-f-strings
Python F-String: 73 Examples to Help You Master It
July 2, 2022 - If you don't specify anything, the float variable will use the full precision. ... Python f-strings have a very convenient way of formatting percentage. The rules are similar to float formatting, except that you append a % instead of f. It ...
Find elsewhere
🌐
AskPython
askpython.com › python › string › python-floating-point-formatting
Python Floating Point Formatting: 2 Simple Methods - AskPython
January 18, 2024 - You can limit a float to 2 decimal places using either f-strings or the format() method.
🌐
DZone
dzone.com › coding › languages › python f-strings
Python F-Strings - DZone
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.
🌐
Codingem
codingem.com › home › f-strings in python: a complete guide (with examples)
F-Strings in Python: A Complete Guide (with Examples)
December 5, 2022 - To specify the desired precision of a floating-point number, use a colon (:) followed by the number of digits you want to see in the output. For instance, let’s print a float with 2 decimal accuracy using an F-string: f_num = 12.3241233 ...
🌐
Pybites
pybit.es › articles › python-f-string-codes-i-use-every-day
Python F-String Codes I Use Every Day – Pybites
April 5, 2024 - 💡 Side note: f-strings are flexible enough to allow nesting. float_val = 1.5 precision = 3 print(f"{float_val:.{precision}f}") The output: 1.500 · Use code % to enforce percentage type. Percentage is still a float so you can still use .<whole_number> to control the precision.
🌐
Reddit
reddit.com › r/learnpython › pythonic way to show 2 decimal places in f-strings?
r/learnpython on Reddit: Pythonic way to show 2 decimal places in f-strings?
March 1, 2019 -

I've got a number that needs to be rounded to 2 decimal places. Getting the round is easy, but if the number is a whole number or only has 1 digit beyond the decimal the rounding doesn't properly show 2 decimal places.

answer = 1
print(round(float(answer),2))

>> 1.0

I really like f-strings, so I would use this:

answer = 1
print(f"{round(float(answer),2):.2f}")

>> 1.00

Is there a neater or more readable method of doing this?

🌐
Bentley
cissandbox.bentley.edu › sandbox › wp-content › uploads › 2022-02-10-Documentation-on-f-strings-Updated.pdf pdf
A Guide to Formatting with f-strings in Python - CIS Sandbox
type as n or d and use spacing in the output. When variable = math.pi, the f-string · understands that variable is a floating-point number and displays it as such.
🌐
Finxter
blog.finxter.com › home › learn python blog › python convert float to string
Python Convert Float to String - Be on the Right Side of Change
March 9, 2024 - For example, the expression '{:.2f}'.format(x) converts the float variable x to a float with precision 2 such as 3.13 or 1.23. ... To set the precision after the comma to two decimal places when converting a float to a string, you can use Python’s f-string formatting functionality.
🌐
Saralgyaan
saralgyaan.com › posts › f-string-in-python-usage-guide
Python f-strings - The Ultimate Usage Guide
August 20, 2019 - ladbrokes立博-公司官网 版权所有 地址:ladbrokes立博 邮编:250100 电话:0534-8985830
🌐
Scaler
scaler.com › home › topics › f string in python
F-String in Python - String Interpolation and Formatting | Scaler Topics
May 13, 2022 - Floating-point values in Python can be displayed in various ways - with different positioning of the decimal point. f-string in Python helps us specify the precision of the floating-point value and display it accordingly.
🌐
Cjtu
cjtu.github.io › spirl › python_str-formatting.html
3.10. String Formatting (Interactive) — Scientific Programming<br>In Real Life
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.
🌐
Real Python
realpython.com › python-f-strings
Python's F-String for String Interpolation and Formatting – Real Python
November 30, 2024 - The f letter tells the operator to convert to a floating-point number. The .2 part defines the precision to use when converting the input. In the second example, you use %5s to align the age value five positions to the right.