Type conversions
In the previous lesson every token carried a type stamp that cannot be changed. But you often need a token of a different kind with the same content. Picture a workshop: you bring a text token that says "5", the craftsman reads it and mints a new token—the number 5. Your old token stays exactly as it was.
Why do you need this? A number written in quotes is a string, and + joins such values instead of adding them. To get a sum, you first turn the strings into numbers:
a = "5"
b = "3"
print(a + b) # 53
print(int(a) + int(b)) # 8Let's walk through the example line by line:
a = "5"andb = "3"tie tags to two strings.print(a + b)joins the strings and prints53.int(a)returns a new number5, andint(b)returns the number3.- Adding two
intvalues gives8, andprintprints it.
The int, float and str functions
A type conversion is written as a call to a function named after the type you need: int(x), float(x) or str(x). The function returns a new object of that type. The variable x itself does not change and still refers to the old object.
print(int("7") + 1) # 8
print(float("2.5")) # 2.5
print(float(3)) # 3.0
print(str(42) + "!") # 42!
print(int(2.9), int(-2.9)) # 2 -2Let's take each function separately.
int(), a whole number
int("7") reads the string and returns the number 7, so int("7") + 1 gives 8.
Applied to a fractional number, int() drops the fractional part instead of rounding. So int(2.9) is 2, and int(-2.9) is -2. Interviewers like to ask what int(-2.9) returns: the answer is -2, not -3.
float(), a fractional number
float("2.5") turns the string into the number 2.5. float() also accepts a string with a whole number: float("4") gives 4.0.
float(3) gives 3.0: a fractional number prints with a point even when it is whole.
str(), a string
str(42) turns the number into the text "42", which can be joined with another string. So str(42) + "!" prints 42!.
When a conversion fails
int() accepts a string only if it holds a whole number. It does not parse a string with a point:
print(int("2.5")) # ✗ValueError: invalid literal for int() with base 10: '2.5'A ValueError means the argument's type fits but its value does not. int() accepts a string, but the text "2.5" does not look like a whole number. The craftsman from our picture cannot read such a label as a whole number, and the program stops at this line. The craftsman is strict: he does not guess what you meant, and a single extra point is already in the way.
You can get the number 2 from "2.5" in two steps: int(float("2.5")). First float() gives 2.5, then int() drops the fractional part.
Keyboard input
The input() function reads a line that a person types on the keyboard. It always returns a str, even when only digits were typed. That is why its result is wrapped in int() or float() before any arithmetic.
In the course's exercises, data comes into a function as arguments, and input() is not needed there.
Common mistake
A string is joined with a number without a conversion:
total = 8
print("Total: " + total) # ✗TypeError: can only concatenate str (not "int") to str+ does not turn a number into text on its own: a string can be joined only with a string. A TypeError means the operation does not fit this combination of types. There are two ways to fix it:
- turn the number into text:
"Total: " + str(total); - pass the values to
printseparated by a comma:print("Total:", total).
Key points
int(x),float(x)andstr(x)return a new object of the type you need, andxdoes not change.int()of a fractional number drops the fractional part:int(-2.9)is-2.int("2.5")raises aValueError, and"Total: " + 8raises aTypeError.input()always returns a string, so it is converted to a number for arithmetic.
Subscribe to take the course
The course is free. Sign in with Telegram and subscribe to the channel to open every lesson.