In this article, we will learn about how to write a python program to read float as input.
There is no such built-in method in python that accepts float value directly from console. But, there is a built-in method – input() which can be used to read string value entered in console. Then, we can convert string value into float using built-in float() function. We can convert entered value to float as below –
float(entered_value)
Example:
float(2.4)
It returns 2.4 as float value.
Example 1: Read Float From Console in Python
Using built-in method input() and data type conversion to float, we can read value to console in python as below –
# Read float input from console n = float(input('Enter a Float value: '))
When we run above problem, we get output as below –
Enter a Float value: 23.0
Here, 23.0 is float value entered by user in console.
Example: Print value in Python
Now, we are going to print float value entered by user in console. We can do so using print() method in python as below –
print(FLOAT_VALUE)
Finally,
# Read float input from console n = float(input('Enter a Float value: ')) print('Entered value is: ', n)
When we run above program, output will be –
Enter a Float value: 23.0 Entered value is: 23.0
Thus, we learnt about how to write python program to read float as input and print it to console. Visit post to learn more about python.