Courses/Python Mastery/Module 3: Variables & Data Types
Module 3 · Lesson 515 minBeginner

Type Conversion (and why input() always bites you)

Lesson goal
Convert between types safely and understand the classic input() trap.

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

code
age = input("Enter your age: ")     # user types 21
print(age + 1)

Output:

code
TypeError: can only concatenate str (not "int") to str

Why? 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:

code
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:

code
int("21")       # 21     — string to integer
float("3.5")    # 3.5    — string to decimal
str(21)         # "21"   — number to string

The corrected program:

code
age = int(input("Enter your age: "))
print(age + 1)      # 22 — real math at last

Read 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

code
# 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)        # 0

Watch 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)

code
int("hello")

Output:

code
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):

code
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:

code
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 automatically

This is a quiet superpower of f-strings: they convert anything automatically. When in doubt, f-string it out.

The conversion cheat sheet

FromToFunctionWatch out for
"21"numberint("21")ValueError on non-numbers
"2.5"decimalfloat("2.5")same
21textstr(21)rarely needed with f-strings
2.9wholeint(2.9)truncates, doesn't round
2.9roundedround(2.9)returns 3

✅ Checkpoint

  • What type does input() always return? *(String)*
  • Fix: 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)*
  • What's the safest way to display a number inside text? *(f-string — auto-converts)*
  • Next: input() and output formatting — reading from users and printing like a professional.