Skip to content
dreamcode
dreamcode
Map
Type conversion
Lesson 6 of 77
+15 XP on finish
PYTHON BASICSChapter 1 · Python Basics

Converting types

Every value has a type: int, float, str or bool. Ask for it with type(value). Text that looks like a number is still text until you convert it with int() or float(), and str() turns anything into text. It matters because "2" + "3" is "23", not 5.

Worked example

How it reads

  • + joins strings but adds numbers, so the type decides what happens
  • int("2") makes the number 2 from the text "2"
  • type(x) tells you what you are holding
Common mistakes
  • int("seven") raises ValueError. Only text made of digits converts.
  • bool("False") is True: any non-empty string counts as true.
Cloud tip: int("3.7") fails. When text might have a decimal point, convert to float first: int(float("3.7")) is 3.
main.py
PYTHON
real Python, runs in your browser
Console
Run your code to see its output here.
YOUR TURN

Change the last line so it prints the real total, 10.

Press Run to check your work.