Type Conversion in Python

In Python, explicit conversion of a value from one data type to another that is called Type Casting.
In Python, implicit conversion of data types during compilation or run-time that is called Type Coercion.

int(x)It converts x to an integer
long(x)It converts x to a long integer
float(x)It converts x to a floating point number
str(x)It converts x to a string
tuple(x)It converts x to a tuple
list(x)It converts x to a list
set(x)It converts x to a set
ord(x)It converts a single character to its integer value
oct(x)It converts an integer to an octal string
hex(x)It converts an integer to a hexadecimal string
chr(x)It converts an integer to a character
unichr(x)It converts an integer to a Unicode character
dict(x)It converts a dictionary if x forms a key-value pair

However, before using type conversions to convert a floating-point number into an integer number, remember that int() converts a float to an int by truncation and not by rounding to the nearest whole number. The round() works more appropriately by rounding a floating-point number to the nearest integer as shown below:

>>> int(5.75)



Another Example:

>>> round(5.75)



The round() can even take a second optional argument which is usually a number that indicates the number of places of precision to which the first argument should be rounded.

>>> round(98.567852)