Python int() built-in function

From the Python 3 documentation

Return an integer object constructed from a number or string x, or return 0 if no arguments are given.

Introduction

The int() function in Python is a built-in function that allows you to convert a value into an integer. It can take a string or a number as an argument and will return the integer equivalent. This is particularly useful when you need to perform mathematical operations that require whole numbers or when you need to ensure that a value is of the integer type.

Examples

>>> # transform a string to an integer
>>> from_integer = int('29')
>>> from_integer
# 29
>>> type(from_integer)
# <class 'int'>

>>> # transform a float to integer
>>> from_float = int(-3.14)
>>> from_float
# -3
>>> type(from_float)
# <class 'int'>

>>> # return an empty string
>>> int()
# 0

Subscribe to pythoncheatsheet.org

Join 16,702+ Python developers in a two times a month and bullshit free publication , full of interesting, relevant links.