What is float 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:  <class 'str'>
Enter another value: 10.23
value of val2:  10.23
type of val2:  <class 'str'>
Enter another value: Hello
value of val3:  Hello
type of val3:  <class 'str'>

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:  <class 'str'>
Enter any number: 789.123
value of val2:  789.123
type of val2:  <class 'float'>

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

The Python float() method converts a number stored in a string or integer into a floating point number, or a number with a decimal point. Python floats are useful for any function that requires precision, like scientific notation.


Programming languages use various data types to store values. The type of data a value is stored as will affect how a program can manipulate that value. For instance, you cannot run a string function on an integer or a mathematical operation on a string.

What is float input in python

Find Your Bootcamp Match

  • Career Karma matches you with top tech bootcamps
  • Access exclusive scholarships and prep courses

Select your interest
First name

Last name

Email

Phone number

By continuing you agree to our Terms of Service and Privacy Policy, and you consent to receive offers and opportunities from Career Karma by telephone, text message, and email.

There are two number types in Python: floating-point numbers (floats) and integers. While floats can contain decimals, integers cannot.

This tutorial will discuss the basics of floating-point numbers and how you can use the float() method to convert strings and integers to floats in Python. We’ll also walk through programming examples that employ the float() method to convert integers and strings to floating-point numbers.

Python Float

Any numerical value entered into Python will be seen as a number, so it’s not necessary to declare that a value is a number. When you insert a value without decimals, Python will interpret it as an integer (e.g., 24 or -72); values that include decimals will be interpreted as floats (e.g., 102.2 or -4.91).

Integers include positive whole numbers (e.g., 3, 7, 26), their negative counterparts (e.g., -3, -7, -26), and 0. In Python, integers are commonly referred to using the term int. Here’s an example of a program that prints out an integer to the console:

Our code returns: 4.

Floating-point numbers, on the other hand, are real numbers. Unlike integers, floating-point numbers can store decimal values.

Here’s a program that prints out a floating-point number to the console:

Our code returns: 3.14.

Because both integers and floats are numbers (in terms of their data type), we can perform mathematical operations on them. So, if we wanted to perform floating-point arithmetic and add two floats together, for example, we could do so using the following code:

math_sum = 22.5 + 17.4
print(math_sum)

Our code computes 22.5 + 17.4, and then returns: 39.9.

Python float() Method

The float() method is a built-in Python function that is used to convert an integer or a string to a floating-point value.

Here’s the syntax for the float() method in Python:

The float() method takes in one parameter: the value you want to convert to a float. This parameter is optional and its default value is 0.0.

Converting an Integer to a Float in Python

You can use the float() method in Python to convert an integer to a floating-point number. Here’s an example:

float_number = float(12)

print(float_number)

Our code returns: 12.0.

Here, we used the float() method to convert an integer (12) into a floating-point number (12.0). The . at the end tells us that our number has successfully been converted to a floating-point value.

Converting a String to a Float in Python

In Python, a string is a sequence of characters.  Just as float() can convert an integer to a floating-point number, it can convert a string to a floating-point number as well. To do so, the string must be numerical.

Here’s an example of float() being used to convert a string to a floating-point number in Python:

float_string = float("12")

print(float_string)

Our code returns: 12.0.

In this example, a numerical value (12) was stored as a string in our program. The float() method converted this value into a floating-point number.

You can use the + and - signs to denote whether you want your string to be converted to a positive float or a negative float. Here’s an example of this in action:

float_string = float("-12")

print(float_string)

Our code returns: -12.0. Notice that our data is now stored as a float, rather than a string. This is evident because our number is not in quotation marks, and ends with .

Strings can also contain NaN, infinity, or inf. These combinations of letters are used to represent invalid numbers (NaN) and infinity values (infinity or inf).

Python Float(): Example

Let’s walk through a more detailed example to show how to use the float() method in Python.

Say that we are creating a math game for fourth-grade students who are learning about adding and subtracting decimal numbers. This game will give students a math problem to solve and will evaluate whether their responses are accurate. Because we are working with decimals, we’ll want our code to convert each number to a float.

What is float input in python

"Career Karma entered my life when I needed it most and quickly helped me match with a bootcamp. Two months after graduating, I found my dream job that aligned with my values and goals in life!"

Venus, Software Engineer at Rockbot

Here’s the code we could use to create this game:

answer = round(12.2 + 14.6, 1)

student_input = input("What is 12.2 + 14.6?")

if float(student_input) == answer:
	print("You're right!")
else:
	print("The correct answer to 12.2 + 14.6 is", answer, ".")

When we run our code and input the correct answer (26.8) in response to the prompt, our code returns the following:

What is 12.2 + 14.6?
26.8
You're right!

When we run our code and input the incorrect answer (for example, -2.4) in response to the prompt, our code returns the following:

What is 12.2 + 14.6?
-2.4
The correct answer to 12.2 + 14.6 is 26.8.

Let’s break down our code. On the first line, we calculate the answer to a math problem (12.2 + 14.6) and use the round() method to make sure we have a value that is rounded to one decimal place.

Then, we use the input() method to ask a student to input an answer to the math problem. The parameter of the input method here is the question our program will ask students (What is 12.2 + 14.6?)

On the next line, we use an if statement to compare whether the student’s answer is equal to the answer our program calculated. Importantly, we use the float() method to convert the variable student_input to a float. We must do this because the input() method returns data in a string format, and we cannot compare a string to a float. So, in order for our program to work, we needed to convert student_input to a float.

If a student enters the correct answer, the message You’re right! is printed to the console; otherwise, the console prints the correct answer, which follows the text we set to precede the answer (The correct answer to 12.2 + 14.6 is).

Conclusion

Floating-point numbers are a crucial part of programming. They allow developers to work with decimal values. This tutorial discussed the basics of floating-point values in Python and walked through a few examples of how to use the float() method to convert integers and strings to floating-point numbers. 

You’re now ready to start working with floating-point numbers and the built-in Python float() method like an expert!