Type Conversion (and why input() always bites you)
You've been warned twice in this course: input() gives you text, not numbers. Time to understand why — and master type conversion, the skill that turns one type into another.
The trap, fully explained
age = input("Enter your age: ") # user types 21
print(age + 1)Output:
TypeError: can only concatenate str (not "int") to strWhy? input() always returns a string. The user's "21" arrived as the text "21" — two characters, not a number. And Python refuses "21" + 1 because adding text and math is meaningless to it.
Prove it to yourself:
age = input("Enter your age: ")
print(type(age)) # <class 'str'> ← even though the user typed digits!The fix: conversion functions
Three functions convert between the core types:
int("21") # 21 — string to integer
float("3.5") # 3.5 — string to decimal
str(21) # "21" — number to stringThe corrected program:
age = int(input("Enter your age: "))
print(age + 1) # 22 — real math at lastRead it inside-out: input() gets the text → int() converts it → the result is stored in age. This exact line appears in thousands of beginner programs, including several in this course.
Conversions in every direction
# string ↔ number
int("42") # 42
float("2.5") # 2.5
str(42) # "42"
str(2.5) # "2.5"
# float ↔ int
int(2.9) # 2 ← CHOPS the decimal, no rounding!
int(2.1) # 2
round(2.9) # 3 ← round() actually rounds
# bool ↔ number
int(True) # 1
int(False) # 0Watch the int(2.9) case: conversion truncates (cuts off), it doesn't round. int(2.9) is 2, not 3. If you want rounding, that's what round() is for.
When conversion fails (and how to survive it)
int("hello")Output:
ValueError: invalid literal for int() with base 10: 'hello'Python can't turn "hello" into a number — fair enough. This *will* happen to you when a user types "twenty-one" or "abc" into your age prompt. The professional fix uses try/except (coming in Module 8):
try:
age = int(input("Age: "))
print(f"Next year you'll be {age + 1}")
except ValueError:
print("That's not a number!")For now, know that this error means "the text couldn't be converted."
The other direction: str() for display
The reverse trap — joining numbers into text:
age = 21
print("I am " + age) # TypeError!
print("I am " + str(age)) # "I am 21" ✅
print(f"I am {age}") # "I am 21" ✅ — f-strings convert automaticallyThis is a quiet superpower of f-strings: they convert anything automatically. When in doubt, f-string it out.
The conversion cheat sheet
| From | To | Function | Watch out for |
|---|---|---|---|
| "21" | number | int("21") | ValueError on non-numbers |
| "2.5" | decimal | float("2.5") | same |
| 21 | text | str(21) | rarely needed with f-strings |
| 2.9 | whole | int(2.9) | truncates, doesn't round |
| 2.9 | rounded | round(2.9) | returns 3 |
✅ Checkpoint
input() always return? *(String)*age = input("Age: ") then print(age * 2) — user typed 10. What prints, and how do you fix it? *(Prints "1010" — string repetition! Fix: age = int(input(...)), then 20)*int(2.9) = ? *(2 — truncation, not rounding)*Next: input() and output formatting — reading from users and printing like a professional.