The unofficial CircuitPython Reference

int()

description

Python provides builtin functions that explicitly convert between data types. This is commonly known as casting. It is often necessary to cast values when they are used as arguments in a function call. It is generally necessary to cast values whenever a function or other process cannot implicitly convert a given variable to the appropriate datatype. For example when printing a string followed by a numeric type the numeric type must be cast to a string for string concatenation in the print() call to work.

int(variable)  is used to cast values to integers. Casting a float to an int will disregard its decimal places. Casting a string to int will convert numeric strings like "123" to integer values like 123.

syntax

cast float to int

my_var = 16.05

int(my_var)

cast string to int

my_string = "1605"

int(my_string, 10)

methods

parameters

int(value, base)

value : a numeric or string-numeric value

base : optional integer to indicate base of numeric-string value (default is base 10)

returns

example code

serial output:

code.py output:

16

1605

see also: