Python Type Conversion & Casting
Here is a classic coding trap: If you ask Python to add "50" + "50" (strings with quotes), it gives you "5050" instead of 100! To do real math with text numbers, you must convert them using int("50") or float("3.14"). This is called type casting.
"Imagine receiving a package labeled "50". Until you open the box and treat it as a real number, it is just cardboard with ink on it. Type casting opens the box so you can do math with it."
Deep Dive: How It Works
Implicit Conversion: Python automatically converts integers to floats during division (e.g. 10 / 2 becomes 5.0).
Explicit Casting: You manually convert data using Python built-in functions:
int(x): Converts text or float to an integer by chopping off decimals (e.g. int("42") is 42, int(9.99) is 9).
float(x): Converts text or integer into a decimal float (e.g. float("19.95") is 19.95, float(5) is 5.0).
str(x): Converts numbers or booleans to text strings (e.g. str(100) is "100").
Syntax Blueprint
num = int("42") # Converts text "42" to integer 42
price = float("19.99") # Converts to float 19.99
text = str(100) # Converts 100 to text "100"Wrap your variable or literal in int(), float(), or str() to convert its type.
Core Rules to Remember



Common Beginner Traps & How to Fix Them
Trying to int() non-numeric text: int("banana")Why it happens: Python cannot magically turn words into numbers and throws a ValueError.
How to fix: Ensure strings contain valid digits before passing them to int() or float().
Live Interactive Example
Hit Run Code to see it liveYour Turn: Micro Challenge
No pressure! Edit the starter code below and test your solution with instant feedback.
Convert a string to an integer and multiply
Given quantity_str = "8", convert it to an integer.
Multiply it by 5 and print the result.
Finished reading and practicing?
Mark this lesson as completed to update your course progress.