Python3 Data Type Conversion
Sometimes, we need to convert between built-in data types. In general, you just need to use the data type name as a function.
Python data type conversion can be divided into two types:
- Implicit Type Conversion - automatic
- Explicit Type Conversion - requires using type functions to convert
Implicit Type Conversion
Section titled “Implicit Type Conversion”In implicit type conversion, Python automatically converts one data type to another without our intervention.
In the following example, we perform operations on two different types of data. The lower data type (integer) is converted to the higher data type (float) to avoid data loss.
Example
Section titled “Example”The above example outputs:
Code explanation:
- In the example, we add two variables of different data types,
num_intandnum_flo, and store the result in the variablenum_new. - Then we check the data types of the three variables.
- In the output, we see that
num_intisinteger, andnum_floisfloat. - Likewise, the new variable
num_newisfloat, because Python converts the smaller data type to the larger data type to avoid data loss.
Let’s look at another example, adding an integer and a string:
Example
Section titled “Example”The above example outputs:
As can be seen from the output, operations between integer and string types result in an error, outputting a TypeError. Python cannot use implicit conversion in this case.
However, Python provides a solution for these situations, called explicit conversion.
Explicit Type Conversion
Section titled “Explicit Type Conversion”In explicit type conversion, the user converts the object’s data type to the desired data type. We use predefined functions such as int(), float(), str(), etc., to perform explicit type conversion.
int() forces conversion to integer:
Example
Section titled “Example”float() forces conversion to float:
Example
Section titled “Example”str() forces conversion to string:
Example
Section titled “Example”Operations between integer and string types can be accomplished using explicit type conversion:
Example
Section titled “Example”The above example outputs:
The following built-in functions can perform conversions between data types. These functions return a new object representing the converted value.
| Function | Description |
| int(x [,base]) | Converts x to an integer |
| float(x) | Converts x to a floating-point number |
| complex(real [,imag]) | Creates a complex number |
| str(x) | Converts object x to a string |
| repr(x) | Converts object x to an expression string |
| eval(str) | Evaluates a valid Python expression in a string and returns an object |
| tuple(s) | Converts sequence s to a tuple |
| list(s) | Converts sequence s to a list |
| set(s) | Converts to a mutable set |
| dict(d) | Creates a dictionary. d must be a sequence of (key, value) tuples |
| frozenset(s) | Converts to an immutable set |
| chr(x) | Converts an integer to a character |
| ord(x) | Converts a character to its integer value |
| hex(x) | Converts an integer to a hexadecimal string |
| oct(x) | Converts an integer to an octal string |
| bool(x) | Converts object x to a boolean value (True or False) |
| bytes([source[, encoding[, errors]]]) | Converts an object to an immutable byte sequence |
| bytearray([source[, encoding[, errors]]]) | Converts an object to a mutable byte array |
| memoryview(obj) | Returns a memory view object of the given argument (without copying data) |
| bin(x) | Converts an integer to a binary string |
| ascii(x) | Returns the ASCII representation of an object; non-ASCII characters are escaped |