Turning "17" into 17: int(), float() and str(), why int(7.9) is 7 and not 8, and the one-line input recipe every program uses.
01
Learning objectives
Convert values with int(), float() and str() and predict each result.
State that int() truncates toward zero — int(7.9) is 7, int(-7.9) is -7 — while round() rounds.
Explain why input() always returns a str and cast it before arithmetic.
Predict the ValueError raised by int("abc") and the TypeError from "17" + 1.
Reconstruct the standard recipe: number = int(input(prompt)).
02
Lesson notes
Values of different types behave differently — lesson 2.1 showed "2" + "3" making "23" while 2 + 3 makes 5. Type conversion (or casting) is the deliberate act of producing a new value of a chosen type from an existing one, using three built-in functions: int(), float() and str(). Each returns a NEW value of the target type; the original value and variable are untouched unless you assign the result back. That last clause is the exam trap: str(x) on its own changes nothing — x = str(x) is what rebinds the name.
int() converts floats and numeric strings to whole numbers, and its behaviour on floats is tested every year: it truncates — chops the fraction toward zero — it does NOT round. So int(7.9) is 7, int(7.1) is also 7, and int(-7.9) is -7. When you actually want nearest-value rounding, that is the separate round() function: round(7.6) is 8. Converting strings follows the same spirit: int("45") is the integer 45, but int("45.5") raises a ValueError — int() refuses decimal text; go through float first: int(float("45.5")).
float() accepts ints and numeric strings (float(7) → 7.0, float("3.14") → 3.14), and str() accepts anything (str(88) → "88", str(True) → "True"). Conversions fail loudly when the text is not a number at all: int("abc") is a ValueError. Failing loudly is a feature — chapter 9's try/except exists precisely to catch these moments and ask the user again instead of crashing.
The reason casting earns a whole lesson is one function: input(). Whatever the user types — digits, letters, spaces — input() hands you a str, always. Ask for an age with age = input("Age: ") and then write age + 1, and Python raises TypeError: can only concatenate str — you tried to add a number to text. The universal recipe, used in virtually every stdin-style challenge in the Labs, is one line: age = int(input("Age: ")) — read text and cast it in the same breath.
Casting also polishes output. Adding a float to a message needs str() or, better, an f-string (next module): "Result: " + str(percentage) works, and f"Result: {percentage}" reads nicer. And remember the reverse direction — str(4.0) is "4.0", keeping the decimal point, because str() preserves the value's exact form. Trace every conversion as a two-column table (before → after) in your notebook; the habit turns the whole topic into free marks.
03
Worked code examples
int() truncates, round() rounds
int() chops toward zero (7.9 → 7, -7.9 → -7); round() picks the nearest value (7.9 → 8, 7.4 → 7). int("45") turns text into the integer 45, so 45 + 5 = 50.
The program is fed 12.5 and then 4 (shown as Program input). Both arrive as text; casting at the same line makes the multiplication legal, and price × qty computes 50.0 as a float.
Python refuses to guess whether you wanted 18 or "171". Cast explicitly: int("17") + 1 gives 18, or "17" + str(1) gives "171" — your choice, stated in code.
print("17" + 1)
Output
TypeError: can only concatenate str (not "int") to str
04
Key terminology
casting / type conversion
Producing a new value of a chosen type from an existing one with int(), float() or str().
truncate
To drop the fractional part toward zero — int(7.9) is 7, int(-7.9) is -7.
ValueError
The runtime error when a conversion receives text that cannot be parsed, e.g. int("abc").
TypeError
The runtime error when an operation mixes incompatible types, e.g. "17" + 1.
round()
The built-in that rounds to the nearest value — the correct tool when int()'s truncation is not what you mean.
05
Common misconceptions
Common mistake
int(7.9) rounds up to 8.
Correct understanding
int() truncates: the fraction is discarded, giving 7. Rounding is round()'s job.
Common mistake
str(marks) changes the variable marks into a string.
Correct understanding
It returns a NEW string value; marks itself is unchanged unless you assign: marks = str(marks).
Common mistake
int("45.5") is 45.5 or 45.
Correct understanding
int() rejects decimal text with a ValueError. Convert through float first: int(float("45.5")).
06
Check your understanding
1. What is the value of int("45") + 5?
2. Which statement converts the string "45" into the integer 45?