Can you float an input in python?

Home » Python

Python | read/take input as a float: Here, we are going to learn how to read input as a float in Python?
Submitted by IncludeHelp, on April 02, 2019

To take input in Python, we use input() function, it asks for an input from the user and returns a string value, no matter what value you have entered, all values will be considered as strings values.

Consider the following example,

# python code to demonstrate example
# of input() function

val1 = input("Enter any value: ")
print("value of val1: ", val1)
print("type of val1: ", type(val1))

val2 = input("Enter another value: ")
print("value of val2: ", val2)
print("type of val2: ", type(val2))

val3 = input("Enter another value: ")
print("value of val3: ", val3)
print("type of val3: ", type(val3))

Output

Enter any value: 10
value of val1:  10
type of val1:  
Enter another value: 10.23
value of val2:  10.23
type of val2:  
Enter another value: Hello
value of val3:  Hello
type of val3:  

See the program and output – Here, we provided three value "10" for val1 which is an integer value but considered as a string, "10.23" for val2 which is a float value but considered as a string, "Hello" for val3 which is a string value.

How to take input as a float?

There is no such method, that can be used to take input as a float directly – but input string can be converted into a float by using float() function which accepts a string or a number and returns a float value.

Thus, we use input() function to read input and convert it into a float using float() function.

Consider the below example,

# python code to take float input

# reading a value, printing input and it's type
val1 = input("Enter any number: ")
print("value of val1: ", val1)
print("type of val1: ", type(val1))

# reading a value, converting to float
# printing value and it's type
val2 = float(input("Enter any number: "))
print("value of val2: ", val2)
print("type of val2: ", type(val2))

Output

Enter any number: 123.456
value of val1:  123.456
type of val1:  
Enter any number: 789.123
value of val2:  789.123
type of val2:  

Example to input two float numbers and find their sum and average

# python code to read two float numbers
# and find their addition, average

num1 = float(input("Enter first number : "))
num2 = float(input("Enter second number: "))

# addition
add = num1 + num2

# average
avg = add/2

print("addition: ", add)
print("average : ", avg)

Output

Enter first number : 123.456
Enter second number: 789.02
addition:  912.476
average :  456.238

Table of Contents

  • Python 3.x example
  • Python 2.x example
    • Was this post helpful?

In this tutorial, we will see how to take float input in Python

There is difference in how you can take input in python 2.x and 3.x. You have to use raw_input in python 2.x and input in Python 3.x

In Python 3.x, raw_input was renamed to input and the Python 2.x input was removed.

💡 Did you know?

raw_input always returns a String object and same is the case with input in Python 3.x

Let’s see with the help of example.

x=input("Enter a float:")

print("value of x: ",x)

print("type of x: ",type(x))

Output:

Enter a float:23.43
value of x: 23.43
type of x:

if you want to take float as input, then you need to use float() function to explicitly convert String to float.


Python 3.x example

x = float(input(“Enter a float: “))
y = float(input(“Enter a float: “))

Let’s understand with the help of example.

x=float(input("Enter an float: "))

y=float(input("Enter an float: "))

print("Sum of x and y:",x +y)

print("Multiplication of x and y:",x *y)

Output:

Enter an float: 23.43
Enter an float: 34.32
Sum of x and y: 57.75
Multiplication of x and y: 804.1176


Python 2.x example

p = float(raw_input(“Enter a float: “))
q = float(raw_input(“Enter a float: “))

Let’s understand with the help of example.

p=float(raw_input("Enter an float: "))

q=float(raw_input("Enter an float: "))

print("Sum of p and q:",p +q)

print("Multiplication of p and q:",p *q)

Output:

Enter an float: 10.34
Enter an float: 20.21
Sum of p and q: 30.55
Multiplication of p and q: 208.97140000000002

If user does not provide float input, then it will raise ValueError: could not convert string to float.
Let’s see with the help of example:

Enter an float: 23.41
Enter an float: NA
Traceback (most recent call last):
File “main.py”, line 2, in


q = float(input(“Enter an float: “))
ValueError: could not convert string to float: ‘NA’

That’s all about How to take float input in Python.

How do you convert input to float in Python?

We can convert a string to float in Python using the float() function. This is a built-in function used to convert an object to a floating point number. Internally, the float() function calls specified object __float__() function.

How do you float a number in Python?

To convert the integer to float, use the float() function in Python. Similarly, if you want to convert a float to an integer, you can use the int() function.

How do you create a float in Python?

00:51 The simplest way to define a floating-point number in Python is to create a variable with a decimal point and a number after it, such as seen here, where we have a = 4.2 . 01:03 You can see that the value is 4.2 , and entering type(a) shows a and a floating-point number has been created.