Skip to content
masterclassBuddyTech Buzz5 fresh blogs · 5:00 AM IST

Python · Class

float

float constructs a floating-point number from a number or string. It also accepts no argument, returning 0.0.

How it works

float constructs a floating-point value from a number or a string. A string may contain a signed decimal value with surrounding whitespace; omitting the argument produces 0.0.

Syntax

float(x=0.0)

Parameters

NameTypeDescription
xnumber or stringA number or string to convert; omit it to obtain 0.0.

Returns

A floating-point number; with no argument, 0.0.

Key points

  • The built-in constructs a floating-point number from numeric or textual input.
  • Calling float without an argument yields 0.0.
  • A string may begin with either sign; plus does not change the magnitude.
  • Scientific notation in a string is accepted and converted to its numeric value.
  • The documented string forms include signed infinity, which produces negative infinity.

Common mistakes

  • String input must represent a decimal number, with an optional sign and surrounding whitespace; invalid text cannot be converted.
  • Integer and floating-point inputs preserve their value only within Python's floating-point precision.

Example: Construct floating-point values

value = float("1.25")
whole = float(3)
default = float()
print(value, whole, default)

The snippet converts a decimal string and an integer, then shows the zero-argument default.

When to use it

Parse decimal input

Convert request, environment, or command-line text into numeric values before calculations.

Numeric coercion

Normalize integer and floating-point values to one numeric representation.

Default initialization

Create a zero-valued floating-point default when an optional numeric value is absent.

Interview questions

QWhat does float() do?

float() creates a floating-point number from a number or string. With no argument, it returns 0.0.

Follow-up: What does float(3) do? It returns a floating-point value with the same value, subject to floating-point precision.

QWhat string formats can float accept?

A string argument may contain a decimal number, an optional leading sign, and whitespace. Examples include values such as '+1.23' and ' -12345'.

Follow-up: What happens with nonnumeric text? Conversion fails because the string does not describe a valid decimal number.

QHow does float handle numeric inputs?

For an integer or another floating-point value, float returns a floating-point number with the same value within Python's floating-point precision. No argument returns 0.0.

Follow-up: Why can decimal arithmetic differ from expectations? Floating-point precision limits how values are represented and preserved.

  • int
  • complex
  • str
  • round
  • abs

Source: Built-in Functions — Python 3.11.15 documentation. Checked 2026-10-16.