Well I would atleast clean it up as follows:

print "%.2f kg = %.2f lb = %.2f gal = %.2f l" % (var1, var2, var3, var4)
Answer from Frank Krueger on Stack Overflow
Discussions

python - How to print a float variable using the % formatting? - Stack Overflow
Bring the best of human thought and AI automation together at your work. Explore Stack Internal ... I'm using Python 3.7.1 on Windows. When I print a float variable using the % formatting. only the natural part is printed. More on stackoverflow.com
🌐 stackoverflow.com
How can I control the number of decimals when printing a float?
value = 123.456789 for p in range(1, 11): print(f'{value:.{p}f}') This prints: 123.5 123.46 123.457 123.4568 123.45679 123.456789 123.4567890 123.45678900 123.456789000 123.4567890000 More on reddit.com
🌐 r/learnpython
8
4
August 5, 2021
Printing float objects
does anyone know how to print float objects within a string … i am trying to send a string to a function that will print and it is coming back as objecttype float 64 instead of printing the actual number … does anyone know why this is happening … it doesn’t happen with ints … this ... More on numba.discourse.group
🌐 numba.discourse.group
0
0
October 20, 2023
printing - Python print a float with a given number of digits - Stack Overflow
I have some floating point values in Python. I would like to print them so that the have the same number of digits (sum of the integer and decimal parts) For example considering the two numbers: ... 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 › 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
🌐
Programiz
programiz.com › python-programming › methods › built-in › float
Python float()
Python input() The float() method returns a floating point number from a number or a string. int_number = 25 · # convert int to float float_number = float(int_number) print(float_number) # Output: 25.0 ·
Top answer
1 of 4
11

There are several options to evaluate expressions and print them as a string in python.

There are already some good answers, but here are some explicit examples and links to the documentation.

Formatted string literals (f-strings)

f-strings allow you to input expressions which are evaluated at run-time. In the f strings expressions are encased by curly brackets.

As an example:

x = 42.222222222

print(f'My value is: {x}')

prints My value is: 42.222222222.

and with specifying the format:

x = 42.222222222

print(f'My value is: {x:.2f}')

prints My value is: 42.22.

Str formatting method

Strings have a built-in .format() method where you can specify replacement fields with curly brackets.

As an example:

x = 42.222222222

print('My value is: {}'.format(x))

prints My value is: 42.222222222.

and with string formatting:

x = 42.222222222

print('My value is: {:.2f}'.format(x))

prints My value is: 42.22.

String formatting operator

String formatting operator

As an example:

x = 42.222222222

print('My value is: %' % x)

prints My value is: 42.222222222.

and with string formatting:

x = 42.222222222

print('My value is: %.2f' % x)

prints My value is: 42.22.

See @Felk answer for some more qualitive descriptions of the different methods.

2 of 4
3

As you try print float number, use %f instead of %d. This code will print the number to 4 decimal places:

result_2 = 4.523529411764706
statement_2a = "Your text contains an average length of %.4f letter(s) per words." % result_2

print(result_2)
print(statement_2a)
🌐
GeeksforGeeks
geeksforgeeks.org › python › float-in-python
float() in Python - GeeksforGeeks
September 18, 2025 - Example: In this example, we passed ... to float() function. ... Example: This code raises an OverflowError because 10**309 exceeds Python’s floating-point range. ... Traceback (most recent call last): File "/home/1eb6a2abffa536ccb1cae660db04a162.py", line 1, in <module> print(float(10**309)) ...
🌐
DataCamp
campus.datacamp.com › courses › data-types-in-python › numeric-data-types-booleans-and-sets
Printing floats | Python
However, we can use the f strings we learned about previously to make sure we get them printed properly every time by using a format specifier. For example, if we wanted to format a variable in an f string as a float, we can use the f format specifier, such as: print(f"{some_variable:f}").
Find elsewhere
🌐
Python documentation
docs.python.org › 3 › tutorial › floatingpoint.html
15. Floating-Point Arithmetic: Issues and Limitations — Python 3.14.3 documentation
On most machines, if Python were to print the true decimal value of the binary approximation stored for 0.1, it would have to display: >>> 0.1 0.1000000000000000055511151231257827021181583404541015625 · That is more digits than most people find useful, so Python keeps the number of digits manageable by displaying a rounded value instead:
🌐
AskPython
askpython.com › home › python floating point formatting: 2 simple methods
Python Floating Point Formatting: 2 Simple Methods - AskPython
April 10, 2025 - We can take an example to understand better. financial_value = 9876.54321 formatted_financial_value = f”${financial_value:,.2f}” print(formatted_financial_value) ... We can see, how Python provides clarity in reading the values by adding a dollar sign and, commas for thousands of separations and two decimal places.
🌐
Appdividend
appdividend.com › how-to-format-float-values-in-python
How to Format Float Values in Python
December 13, 2025 - # Define a floating-point value value = 123.45678 # Format the value to two decimal places formatted_value = "%.2f" % value print(formatted_value) # Output: 123.46 # Format the value to one decimal place formatted_value = "%.1f" % value print(formatted_value) # Output: 123.5 # Format the value to no decimal places formatted_value = "%d" % value print(formatted_value) # Output: 123
🌐
Numba Discussion
numba.discourse.group › support: how do i do ...?
Printing float objects - Support: How do I do ...? - Numba Discussion
October 20, 2023 - does anyone know how to print float objects within a string … i am trying to send a string to a function that will print and it is coming back as objecttype float 64 instead of printing the actual number … does anyone know why this is happening … it doesn’t happen with ints … this is an example
🌐
Medium
medium.com › @coucoucamille › float-formatting-in-python-ccb023b86417
Simple Float Formatting in Python | by Coucou Camille | Medium
June 15, 2022 - Python’s built-in format() function allows you to format float in any way you prefer. Syntax: {:.2f}.format(num) for rounding to 2 decimal places. ... Syntax: "{:+.2f}".format(num) for positive sign + ; and "{:-.2f}".format(num) for positive sign - . print("{:+.2f}".format(3.1415926)) >>> '+3.14'print("{:-.2f}".format(8.9998)) >>> '-3.14'
🌐
Mimo
mimo.org › glossary › python › float
Mimo: The coding platform you need to learn Web Development, Python, and more.
Start your coding journey with Python. Learn basics, data types, control flow, and more ... How to Create a Float: You can create a float by including a decimal point in a number or by dividing two integers. ... # Creating a float directly my_float = 3.14 print(type(my_float)) # Outputs: <class 'float'> # A float is also created through division result = 10 / 2 print(result) # Outputs: 5.0 print(type(result)) # Outputs: <class 'float'>
🌐
Finxter
blog.finxter.com › home › learn python blog › 5 best ways to format and print lists of floats in python
5 Best Ways to Format and Print Lists of Floats in Python - Be on the Right Side of Change
February 24, 2024 - float_list = [3.14159265, 1.6180339, 2.7182818] formatted_list = ['{:.2f}'.format(i) for i in float_list] print(formatted_list) ... This approach uses a list comprehension to create a new list of strings, with each float formatted to two decimal ...
🌐
W3Schools
w3schools.com › python › ref_func_float.asp
Python float() Function
Python Examples Python Compiler Python Exercises Python Quiz Python Challenges Python Server Python Syllabus Python Study Plan Python Interview Q&A Python Bootcamp Python Certificate Python Training ... The float() function converts the specified value into a floating point number. ... If you want to use W3Schools services as an educational institution, team or enterprise, send us an e-mail: sales@w3schools.com
🌐
iO Flood
ioflood.com › blog › python-float
Python Float: Guide to Decimal Numbers
February 10, 2024 - In Python, declaring and using floats is straightforward. You simply assign a decimal number to a variable. Here’s an example: # declaring a float pi = 3.14 print(pi) # Output: # 3.14
🌐
Finxter
blog.finxter.com › how-to-print-a-float-without-scientific-notation-in-python
How to Print a Float Without Scientific Notation in Python? – Be on the Right Side of Change
The first argument is the float to be displayed. The optional trim argument when set to '-' trims trailing zeros and any trailing decimal point if not needed. ... import numpy as np my_float = 0.00001 print(np.format_float_positional(my_float, trim='-')) # 0.00001
🌐
Quora
quora.com › How-do-you-print-decimals-in-Python
How to print decimals in Python - Quora
Answer (1 of 4): I’ll be generous and pretend you’re not a Troll. Python has a wonderful function called print(). You can put whatever you want in the parentheses, or even nothing (that prints a blank line). Suppose x = 3.14159 Then print(x) produces 3.14159 decimals and all.
🌐
Reddit
reddit.com › r/learnpython › why does the int print different than the float
r/learnpython on Reddit: why does the int print different than the float
February 29, 2024 -

a = 5 / float(2.7)

b = int("5") / int(2.7)

print(a, b)

why are we getting two different numbers?

The way I look at it is if "b" can print a float number (because it has a decimal in it) why is it giving a different number than converting the 2.7 as a float?

I am on day 2 of python learning so I apologize if I am not using the proper terminology.