Skip to content
KodeTrail

Camp 2 · Step 9 of 23

Converting between types

int(), float(), str() — moving values between text and numbers, and checking what type anything is.

10 min+50 XP

You now know three types: int, float, str, plus bool. In real programs they constantly need converting — data arrives as text, math needs numbers, output needs text again.

Checking a type

The type() function tells you what you're holding:

Python

That third line matters: "42" in quotes is text, not a number. It looks the same to you — not to Python.

The conversion functions

Python
  • int(x) — to a whole number (note: it chops, never rounds — int(9.99) is 9)
  • float(x) — to a decimal number
  • str(x) — to text

Conversions that make no sense fail loudly:

int("boots")   # ValueError: invalid literal for int()
Checkpoint

What does print("5" + "5") show?

Checkpoint

What is int(7.8)?