LambdaLabTM
Computer Science · Class 11 · Variables & User Input
Type castingForceful⏱️ 8 min read

Forceful Type Casting

In the last lesson Python changed a type on its own, and only ever between numbers. Now you take charge: when Python will not convert for you — turning text into a number, above all — you command the conversion yourself. That is forceful, or explicit, casting — also called explicit type conversion.

1The problem automatic casting will not solve

You met this wall with input(). Whatever the user types comes back as text, and Python refuses to guess that the text was meant as a number. Add two such values and they join end to end instead of adding up:

the_wall.py
a = input()   # user types 10
b = input()   # user types 5
print(a + b)  # '10' + '5'
Output
105

Automatic casting cannot help here — it never mixes text with numbers. To get 15, you have to force the text into a number yourself. Python gives you a small set of functions for exactly that.

2The four casting functions

Each function takes a value and builds a new value of its own type. Pick a value below and try each one — watch which conversions are clean, which lose something, and which Python refuses outright:

Pick a value, then command a conversion
int(9.8)=9int
This one loses information. the .8 is thrown awayint() does not round, it cuts. That is why int(9.8) is 9, not 10.
int(x)

Makes a whole number. From a float it cuts off the decimals — it does not round.

int('50') → 50 · int(9.8) → 9
float(x)

Makes a decimal number. A whole number gains a .0.

float('49.5') → 49.5 · float(50) → 50.0
str(x)

Makes text. Anything at all can become a string.

str(25) → '25' · str(3.5) → '3.5'
bool(x)

Makes True or False. Zero and empty become False; everything else True.

bool(0) → False · bool(5) → True

3The one you will use most: int(input())

Wrapping input() in int() or float() is how nearly every program takes a number from the user. The two calls sit on one line, and the trick is the order Python runs them: inside-out. The inner input() runs first and produces text, then the outer int() forces that text into a number. Take it one step at a time with Next step, or watch the whole thing with Play all:

Python runs the inner call first
age = int(input())
25
you type
input()
'25'str
input() → text
int()
25int
int() → number
step 1 of 4

Two calls on one line. Python starts with the inner one, input().

So age = int(input()) is really three steps in one line: input() gives the text '25', int('25') gives the number 25, and = stores it. Here is the adding program, fixed at last — run it and type a number each time it stops:

fixed_sum.py

4Two things to watch for

int() cuts, it does not round. Converting a float to an int throws the decimal part away — it does not round to the nearest whole number. If you want rounding, that is a different tool (round()):

cut_not_round.py

The text has to spell a number. int() can convert '50', but not 'fifty' and not even '9.8' (that has a decimal point, so it is not a whole number). When it cannot, Python raises a ValueError rather than guess:

valueerror.py
print(int('hello'))
Output
ValueError: invalid literal for int() with base 10: 'hello'
Only convert when you must
A name is meant to be text, so name = input() needs no casting at all. Reach for int() or float() only when you actually need to do maths with the value. Converting for no reason just adds a place for a ValueError to appear.

5Recap

Key Takeaway
Forceful (explicit) casting is you commanding a conversion with int(), float(), str() or bool(). Unlike automatic casting, it can turn text into a number — which is why int(input()) and float(input()) are everywhere. Python runs the inner call first. int() cuts decimals off rather than rounding, and raises a ValueError if the text is not really a number.
Quick Check

Why do we write int(input()) instead of just input() when we want a number?

Quick Check

What is int(9.8) ?

Quick Check

In age = int(input()), which runs first?

Quick Check

What does int('9.8') do?