Skip to content
Theory · Variables and types · lesson 4 of 7

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:

Python
a = "5"
b = "3"
print(a + b)  # 53
print(int(a) + int(b))  # 8

Let's walk through the example line by line:

  1. a = "5" and b = "3" tie tags to two strings.
  2. print(a + b) joins the strings and prints 53.
  3. int(a) returns a new number 5, and int(b) returns the number 3.
  4. Adding two int values gives 8, and print prints 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.

Python
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 -2

Let'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:

Python
print(int("2.5"))  # ✗
TEXT
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:

Python
total = 8
print("Total: " + total)  # ✗
TEXT
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 print separated by a comma: print("Total:", total).

Key points

  • int(x), float(x) and str(x) return a new object of the type you need, and x does not change.
  • int() of a fractional number drops the fractional part: int(-2.9) is -2.
  • int("2.5") raises a ValueError, and "Total: " + 8 raises a TypeError.
  • 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.

Sign in another way