Python · basics

Type Conversion

Turn a value into another type with int(), float(), str() and bool().

Converting to integer


          # string to int
          int("42")
          # ans: 42
          int("100")
          # ans: 100
          # float to int (truncates)
          int(3.14)
          # ans: 3
          int(9.99)
          # ans: 9
        

Converting to float


          # string to float
          float("3.14")
          # ans: 3.14
          # int to float
          float(42)
          # ans: 42.0
          float(0)
          # ans: 0.0
        

Converting to string


          # int to string
          str(42)
          # ans: "42"
          # float to string
          str(3.14)
          # ans: "3.14"
          # bool to string
          str(True)
          # ans: "True"
        

Converting to boolean


          # number to bool
          bool(1)
          # ans: True
          bool(0)
          # ans: False
          bool(-5)
          # ans: True
          # string to bool
          bool("")
          # ans: False
          bool("text")
          # ans: True
        

Boolean from other types


          # list to bool
          bool([])
          # ans: False
          bool([1, 2])
          # ans: True
          # None to bool
          bool(None)
          # ans: False
        

Zero, empty things and None are False; everything else is True.

Exercises

Predict what comes out of the converter, then press →.

Exercises · part 1


          # what is the result?
          int("100")
          # ans: 100
          # what happens?
          int("3.14")
          # ans: ValueError
        

Exercises · part 2


          # float from string?
          float("3.14")
          # ans: 3.14
          # bool from number?
          bool(0)
          # ans: False
          bool(1)
          # ans: True
          bool(-1)
          # ans: True
        

Exercises · part 3


          # bool from string?
          bool("")
          # ans: False
          bool("False")
          # ans: True (any non-empty string is True)
        

Exercises · part 4


          # string from bool?
          str(True)
          # ans: "True"
          # int from bool?
          int(False)
          # ans: 0
          int(True)
          # ans: 1
        

Exercises · part 5


          # what about this?
          int("  42  ")
          # ans: 42 (whitespace is stripped)
          # edge case?
          int("0b101", 2)
          # ans: 5 (binary to int)
        

Exercises · part 6


          # what happens?
          float("inf")
          # ans: inf (infinity)
          # string to int base?
          int("FF", 16)
          # ans: 255 (hex to int)
        

Exercises · part 7


          # float to int?
          int(3.9)
          # ans: 3 (truncates, doesn't round)
          # bool from empty?
          bool({})
          # ans: False
        

Remember

int() truncates

It drops the decimal part: int(9.99) is 9, not 10.

Strings must look right

int("3.14") is a ValueError; use float() first.

Falsy values

0, "", [], {} and None become False; the rest become True.

Other bases

int(text, base) reads binary (2) or hex (16).