What is Float in Python?

Float is one of the basic built-in data types in Python, paired with int and diverse data types. The float function in Python’s standard library transforms an integer or a string of numbers into a float data type. When a string is regarded as valid for converting to a float, the following rules apply.

  • The string must only include integers.

  • Mathematical operators can also be applied between the numbers.

  • The string might be NaN or inf

  • White spaces at the start and finish are always ignored.

Examples for working of “float()”:

As an integer:

print(float(77.9))

Output:

int bi

Floating point:

print(float(8))

Output:

float bi

Floating type string:

print(float("-16.54"))

Output:

flot string bi

For NaN:

print(float("NaN"))

Output:

nan float bi

For inf:

print(float("inf"))

Output:

inf bi