In this Python tutorial, we will discuss Python type conversion and what are its uses. Before proceeding, however, ensure that you have good knowledge of Python data types. Now, let's discuss Python type conversion & type casting.
Python Type Conversion & Type Casting
Type Conversion
Python is a dynamically typed language, meaning you do not need to specify the type of data you assign to a variable. The python interpreter will understand the variable type and assign a data type accordingly. In Python, nonetheless, you get the feature by which you can convert the data type. This is called type conversion. It is a method or process of converting one data type to another. That could be any data type, integer, float, string , list, or dictionary. There are 2 types of type conversion in Python:
- Implicit Type Conversion
- Explicit Type conversion
We will discuss each of them in detail below.
1. Implicit Type Conversion
When the Python interpreter converts a variable's data type, it is known as implicit data conversion. Let's understand it with an example. Suppose that you have 2 variables, var_int and var2_float , where var_int is an integer data type, and var_float is a float data type. Consequently, when you apply a division or subtraction or multiplication, or addition operation on them, you will get a float value. This is because of the implicit type conversion in Python.
Example:
var_int =10 #it is an integer
var_float=10.0 #it is a float
print("The type of var_int is", type(var_int))
print("The type of var_float is ", type(var_float))
add_vars = var_float + var_int #new data type add_vars will be float not int
print("The value of add_vars is ", add_vars, "and its type is ",type(add_vars))
Output
The type of var_int is <class 'int'>
The type of var_float is <class 'float'>
The value of add_vars is 20.0 and its type is <class 'float'>
What if We Apply the + Operator On an Integer and String?
An interesting thing happens when we apply the + operator on two variables, one of which is an integer type and the other is of string type. If we apply the + operator on an integer and string, we will get a TypeError . Let's understand it with an example.
Example
var_int = 69
var_string = "69" # it is an string because 69 is between the " "
print("The data type of var_int is ", type(var_int))
print("The data type of var_string is ", type(var_string))
print( var_string + var_int) #here we are adding and a string 69 with an integer 69
Output
The data type of var_int is <class 'int'>
The data type of var_string is <class 'str'>
Traceback (most recent call last):
File "importt.py", line 7, in <module>
print( var_string + var_int)
TypeError: can only concatenate str (not "int") to str
2. Explicit Type Conversion (a.k.a. Type Casting)
There are pre-defined functions in python, such as str(), int() , float(), list(), dict(), set(), and tuple(), that are used to convert one type of data into another. In explicit data type conversion, you use these functions to convert the data type of variables.
Explicit conversion of Data-types with examples:
var_int = 144
var_float = 144.0
var_str_int = "69"
print("Type of var_int is ", type(var_int))
print("Type of var_float is ", type(var_float))
print("Type of var_str_int is ", type(var_str_int))
Explicit Type Conversion
print("var_int converted into string ", str(var_int) )
print("var_int converted into Float ", float(var_int))
print("var_float converted into integer", int(var_float))
print("var_float converted into str", str(var_float))
print("var_str_int converted into integer", int(var_str_int))
print("var_str_int converted into float", float(var_str_int))
print("var_str_int converted into list", list(var_str_int))
Output
Type of var_int is <class 'int'>
Type of var_float is <class 'float'>
Type of var_str_int is <class 'str'>
var_int converted into string 144
var_int converted into Float 144.0
var_float converted into integer 144
var_float converted into str 144.0
var_str_int converted into integer 69
var_str_int converted into float 69.0
var_str_int converted into list ['6', '9']
Important Points to Remember in Python Type Conversion & Type Casting
- Using type conversion, you can convert a data type to another data type.
- In explicit type conversion, you can only convert those string values to integer or float values that contain only digits.
- Another name for explicit type conversion is type casting.
Conclusion
That was all about Python type conversion & type casting. For the most part, implicit type conversion will do the job. If you need to convert Python data types of variables manually, you can also do so using built-in type conversion functions.