Python print float: How to Print Float Values in Python

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.

Python print float

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.

Syntax

print("{:.2f}".format(float_values))

Example

float_val = 19.211146

formatted_float_value = "{:.2f}".format(float_val)
print(formatted_float_value)

Output

19.21

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)

Output

19.2111

We can see that we printed a float value with four decimal places.

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.

Leave a Comment