Lesson 03
float() — Convert to Floating-Point
Convert integers, booleans, and numeric strings with float(), and see why complex values and non-numeric text fail.
float() converts a possible value into a floating-point number (float).
This lesson focuses on converting integers, booleans, and numeric strings into decimal numbers, while understanding why complex values and non-numeric text fail.
What you'll learn
- The purpose of
float() - Convert int, bool, and numeric strings
- Understand why complex values fail
- Learn how string indexing can still produce valid floats
What does float() do?
float() converts one possible type of value into a floating-point number.
number = float(value)The resulting value always has the float type, even when the number is whole.
Example 1 — Integer → Float (Possible)
An integer becomes the same number with .0.
a = 10
print(a, type(a))
b = float(a)
print(b, type(b))
print(float(100))10 <class 'int'>
10.0 <class 'float'>
100.0The value keeps the same magnitude — only its type changes.
Example 2 — Boolean → Float (Possible)
Booleans convert exactly like they did with int(), but the result is a float.
| Boolean | float() result |
|---|---|
True |
1.0 |
False |
0.0 |
print(float(True)) # 1.0
print(float(False)) # 0.0Example 3 — Complex → Float (Not Possible)
Complex numbers are not real numbers for float().
a = 2 + 3j
# float(a)TypeError: float() argument must be a string or a real number, not 'complex'This is a TypeError because the type itself is unsupported.
Example 4 — String → Float
Unlike int(), float() accepts both integer strings and decimal strings.
Case 1 — Integer string (Possible)
print(float("12"))
print(float("123"))12.0
123.0Case 2 — Decimal string (Possible)
print(float("12.34"))
print(float("34.56"))12.34
34.56This is the biggest difference from int():
| Call | Result | |
|---|---|---|
| Valid | float("12.34") |
12.34 |
| Invalid | int("12.34") |
ValueError |
Example 5 — Invalid string values
These are strings, but they are not numeric literals.
| Input | Result |
|---|---|
"True" |
ValueError |
"2+3j" |
ValueError |
"Python.Java" |
ValueError |
# float("True")
# float("2+3j")
# float("Python.Java")Interactive playground
Select a source type and see whether float() succeeds.
Choose the source type
Python simulation
float(10)
Conversion successful
10.0 float
float conversion playground
Special case — Indexing a complex string
This often confuses beginners.
The entire string "2+3j" is not a float. But individual characters can be.
| Index | Character | float(...) |
|---|---|---|
0 |
"2" |
2.0 |
1 |
"+" |
ValueError |
2 |
"3" |
3.0 |
3 |
"j" |
ValueError |
a = "2+3j"
print(float(a[0])) # 2.0
print(float(a[2])) # 3.0This works because a[0] and a[2] are digit strings, not because Python understands the complex number.
Complete conversion map
| Source | Example | Result |
|---|---|---|
| int | float(10) |
10.0 |
| bool | float(True) |
1.0 |
| complex | float(2+3j) |
TypeError |
| str (integer) | float("12") |
12.0 |
| str (decimal) | float("12.34") |
12.34 |
str ("True") |
float("True") |
ValueError |
str ("2+3j") |
float("2+3j") |
ValueError |
| Digit character | float("2+3j"[0]) |
2.0 |
Practice exercises
Exercise 1 — Convert both values and compare their types.
float(100)
float("100")Both return 100.0, but one starts as an integer and the other as a string.
Exercise 2 — Compare these two lines:
float("12.34")
int("12.34")Why does only the first succeed?
Exercise 3 — Predict the output:
a = "2+3j"
print(float(a[0]))
print(float(a[2]))Expected answer: 2.0 and 3.0.
Quick knowledge check
-
What is
float(10)?
10.0 -
Does
float("12.34")work?
Yes. It converts the numeric string into a floating-point number. -
Does
float(2+3j)work?
No. It raises TypeError because complex values are not accepted.
Summary
float()converts possible values into floating-point numbers.- int, bool, integer strings, and decimal strings all convert successfully.
- complex values raise TypeError.
- Non-numeric strings raise ValueError.
- Key contrast:
float("12.34")is valid.int("12.34")is a ValueError.
Loading playground…