What is a float in python?

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.0


float() 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

Parameter TypeUsage
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 floats

print(float("-13.33"))

# for string floats with whitespaces

print(float(" -24.45\n"))

# string float error

print(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

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 a float 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 a float 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!

What is a float in Python example?

The float type in Python represents the floating point number. Float is used to represent real numbers and is written with a decimal point dividing the integer and fractional parts. For example, 97.98, 32.3+e18, -32.54e100 all are floating point numbers.

What is a float object in Python?

Python float() function is used to return a floating-point number from a number or a string representation of a numeric value.

Is float a data type in Python?

Floating point numbers or floats are another Python data type. Floats are decimals, positive, negative and zero. Floats can also be numbers in scientific notation which contain exponents. In Python, a float can be defined using a decimal point .

What is a float variable?

In programming, a floating-point or float is a variable type that is used to store floating-point number values. A floating-point number is one where the position of the decimal point can "float" rather than being in a fixed position within a number. Examples of floating-point numbers are 1.23, 87.425, and 9039454.2.