01 / 20
A B

Type Conversion
in Python

Master the art of converting between types using int(), float(), str(), and bool() to build flexible, error-free programs.

Python

What Are Data Types?

Every value in Python has a type that tells the interpreter how to store, display, and operate on that value. The four foundational types you will use every day are:

int
Whole numbers: 42, -7, 0
float
Decimal numbers: 3.14, -0.5
str
Text strings: "hello", "42"
bool
True or False: True, False

Why Convert Types?

Python is strongly typed: it will not silently mix incompatible types. If you try to add a string to an integer, you get a TypeError. Type conversion lets you bridge these gaps deliberately.

# This causes a TypeError age = input("Enter age: ") # always returns a str next_year = age + 1 # str + int → ERROR # Fix: convert first next_year = int(age) + 1 # works!

The Conversion Landscape

int float str bool float() int() str() int() float() str() int()/bool()

The Core Rule of Conversion

# Syntax pattern new_value = target_type(original_value) # Examples int("42") # → 42 float(7) # → 7.0 str(3.14) # → "3.14" bool(0) # → False

These are explicit conversions (also called type casting). You, the programmer, decide when and how to convert.

Implicit vs Explicit Conversion

Implicit (Coercion)
Python does it automatically.
Example: 3 + 2.5 promotes 3 to 3.0, result is 5.5.
Only happens between int and float.
Explicit (Casting)
You call a function yourself.
Example: int("42") converts the string "42" to integer 42.
Works across all four types.

Deep Dive: int()

1
From float: int(3.9) → 3. Truncates toward zero (drops the decimal, does not round).
2
From str: int("42") → 42. The string must contain only digits (and an optional leading -).
3
From bool: int(True) → 1, int(False) → 0.
!
Fails on: int("3.14") → ValueError. You must convert to float first, then to int.

Deep Dive: float()

1
From int: float(5) → 5.0. Always safe, just appends .0.
2
From str: float("3.14") → 3.14. Accepts digits, one decimal point, and optional -.
3
From bool: float(True) → 1.0, float(False) → 0.0.
★
More flexible than int(): float("42") works too → 42.0.

Deep Dive: str() and bool()

str(): Never Fails
str(42) → "42"
str(3.14) → "3.14"
str(True) → "True"
Every Python object has a string representation.
bool(): Truthy/Falsy
Falsy values: 0, 0.0, "", None
Everything else is True:
bool(42) → True
bool("hi") → True

How to Apply Type Conversion

1
Identify the current type. Use type(x) to check what you are working with.
2
Determine the target type. What does the next operation require? Math needs int/float, display needs str.
3
Check if the value is convertible. Can "hello" become an int? No. Can "42"? Yes.
4
Call the conversion function. Wrap the value: int(x), float(x), str(x), or bool(x).
5
Store or use the result. The original variable is unchanged unless you reassign it.

Example 1: User Input Calculator

Build a simple program that reads two numbers from the user and prints their sum.

# Step 1: Read input (always returns str) a = input("First number: ") b = input("Second number: ") # Step 2: Convert to float (handles decimals too) a = float(a) b = float(b) # Step 3: Compute result = a + b # Step 4: Display (convert back to str for concatenation) print("Sum: " + str(result))

Example 2: Chaining Conversions

Sometimes you need to convert through an intermediate type. Remember: int("3.14") fails, but you can chain two conversions.

price = "19.99" # Direct to int fails: # int(price) → ValueError! # Chain: str → float → int cents_approx = int(float(price)) print(cents_approx) # → 19 (truncated, not rounded) # To round instead of truncate: rounded = round(float(price)) print(rounded) # → 20

Example 3: Building Strings from Numbers

You often need to include numbers inside messages. Two approaches:

score = 95 total = 100 # Approach 1: str() + concatenation msg = "You scored " + str(score) + "/" + str(total) # Approach 2: f-string (auto-converts) msg = f"You scored {score}/{total}" # Both produce: "You scored 95/100"

The Truthiness Rule

Falsy → False 0 0.0 "" (empty string) None False Truthy → True 42, -1, 7 3.14, -0.5 "hello", "0", "False" [1, 2], {key: val} True

Common Mistakes

✕ int() on a decimal string
int("3.14") raises ValueError. Use int(float("3.14")) instead.
✕ Forgetting input() returns str
Writing x = input() + 1 crashes. Always convert: int(input()).
✕ Expecting int() to round
int(3.9) gives 3, not 4. It truncates toward zero. Use round() to round.
✕ bool("False") surprise
Any non-empty string is True. bool("False") → True, not False.

Conversion Summary Table

Input int() float() str() bool()
42 42 42.0 "42" True
3.14 3 3.14 "3.14" True
"7" 7 7.0 "7" True
"" Error Error "" False
0 0 0.0 "0" False
True 1 1.0 "True" True

Question 1 of 5

What is the output of int(7.9)?

Question 2 of 5

What happens when you run int("3.5")?

Question 3 of 5

What is the result of bool("")?

Question 4 of 5

Given x = input() and the user types 42, what is type(x)?

Question 5 of 5

What does str(3 + 4.0) + "!" evaluate to?