If you define a value without decimals, Python will interpret it as an integer, and if you specify values that include decimals will be interpreted as floating-point numbers. To print float values in Python, use the print() function. The print() is a built-in function that prints the defined message to the screen or standard output device like a
Python console. To print float values with two decimal places in Python, use the str.format() with “{:.2f}” as str. The str.format() function formats the specified value and insert them inside the string’s placeholder. Then use the print() function with the formatted float-string as a string to print the float values. print("{:.2f}".format(float_values)) float_val = 19.211146
formatted_float_value = "{:.2f}".format(float_val)
print(formatted_float_value) We can see that we defined a float_val, a string representing the number with six decimal places. Using print() function and str.format() with “{:.2f}”, we printed the float values up to two decimal values. If we want a float value with four decimal places, then you should use “{:.4f}”.
The format() method returns the formatted string. float_val = 19.211146
formatted_float_value = "{:.4f}".format(float_val)
print(formatted_float_value) We can see that we printed a float value with four decimal places.Syntax
Example
Output
Output
Print list of float values in Python
To print a list of float values, use the list comprehension with “%.2f”.
data_list = [1.1111, 2.1134, 2.444444, 9.00022] float_values = ["%.2f" % x for x in data_list] print(float_values)
Output
['1.11', '2.11', '2.44', '9.00']
All the values are converted into two decimal place values.
That’s it for this tutorial.
Krunal Lathiya is an Information Technology Engineer. By profession, he is a web developer with knowledge of multiple back-end platforms including Python. Krunal has written many programming blogs which showcases his vast knowledge in this field.
If you just want to convert the values to nice looking strings do the following:
twodecimals = ["%.2f" % v for v in vars]Alternatively, you could also print out the units like you have in your question:
vars = [0, 1, 2, 3] # just some example values units = ['kg', 'lb', 'gal', 'l'] delimiter = ', ' # or however you want the values separated print delimiter.join(["%.2f %s" % (v,u) for v,u in zip(vars, units)]) Out[189]: '0.00 kg, 1.00 lb, 2.00 gal, 3.00 l'The second way allows you to easily change the delimiter (tab, spaces, newlines, whatever) to suit your needs easily; the delimiter could also be a function argument instead of being hard-coded.
Edit: To use your 'name = value' syntax simply change the element-wise operation within the list comprehension:
print delimiter.join(["%s = %.2f" % (u,v) for v,u in zip(vars, units)]) Out[190]: 'kg = 0.00, lb = 1.00, gal = 2.00, l = 3.00'The float() method returns a floating point number from a number or a string.
Example
int_number = 25# convert int to float float_number = float(int_number)
print(float_number) # Output: 25.0float() Syntax
The syntax for float() is:
float([x])float() Parameters
The float() method takes a single parameter:
- x (Optional) - number or string that needs to be converted to floating point number
If it's a string, the string should contain decimal points
Float number | Use as a floating number |
Integer | Use as an integer |
String | Must contain decimal numbers. Leading and trailing whitespaces are removed. Optional use of "+", "-" signs. Could contain NaN, Infinity, inf (lowercase or uppercase). |
float() Return Value
The float() method returns:
- Equivalent floating point number if an argument is passed
- 0.0 if no arguments passed
- OverflowError exception if the argument is outside the range of Python float
Example 1: How float() works in Python?
# for integers print(float(10)) # for floats print(float(11.22)) # for string floatsprint(float("-13.33"))
# for string floats with whitespacesprint(float(" -24.45\n"))
# string float errorprint(float("abc"))
Output
10.0 11.22 -13.33 -24.45 ValueError: could not convert string to float: 'abc'Example 2: float() for infinity and Nan(Not a number)?
# for NaN print(float("nan"))print(float("NaN"))
# for inf/infinity print(float("inf")) print(float("InF"))print(float("InFiNiTy"))
print(float("infinity"))Output
nan nan inf inf inf inf