What is Python Type Casting
Type casting, also known as type conversion, is a process of converting the variable of one data type to the other.
For Example:
a = "45"
Suppose “a” is stored as a string, but it may be needed as an integer to perform some operation like addition, to achieve this goal, we can use typecasting.
There are two types of type casting:
- Implicit Type Casting (or Implicit Type Conversion or Upcasting).
- Explicit Type Casting (or Explicit Type Conversion).

Implicit Type Casting
Implicit type casting is performed automatically by the python interpreter. In this, the interpreter’s priority is to avoid data loss. To achieve this, it converts lower data types like an integer to higher data types like float.
The best example for understanding implicit type casting is the addition of a float with an integer. The integer value will be converted to float because python is supposed to convert lower data type to higher data type to avoid data loss.
product_1_Price = 5
product_2_Price = 5.5
sum = product_1_Price + product_2_Price
print(sum)
Now check the data type of each variable.
When addding integer and float, final result is float
print("Product 1 Price data type = ", type(product_1_Price))
print("Product 2 Price data type = ", type(product_2_Price))
print("Sum data type = ", type(sum))
Explicit Type Casting
Explicit type casting is performed manually by the developer. To achieve this, we use inbuilt constructor functions. For example, we use the int() function to convert a float or string into an integer.
Below is the table showing the data types and function used for typecasting:
Datatype | Function used for casting |
---|---|
Integer | int() |
Float | float() |
Complex | complex() |
Boolean | bool() |
String | str() |
List | list() |
Tuple | tuple() |
Set | set() |
Example – Add Two Numbers
The best example to understand explicit type casting is to ask the user to input two numbers and add them. Here, the data type of numbers received by the input function is a string. Python cannot convert them into a float or an integer automatically. We have to do the manual conversion.
first_num = input("Enter first number:")
second_num = input("Enter second number:")
print("Sum = ", float(first_num)+float(second_num))
Examples(Few Type Cast Operations)
Few examples of some of the typecast operations possible:
String to Integer.
a = "45"
type(a)
a = "45"
b = 10
sum = int(a)+b
sum
String to Float.
a = "45"
b = 10.5
sum = float(a)+b
sum
String to Float.
emp_id = 205
emp_name = "Tim"
emp_code = str(emp_id)+"_"+emp_name
emp_code
Integer to Float.
a = 45
float(a)
Float to Integer.
a= 45.89
int(a)
Integer to Complex.
complex(45)
Integer to Boolean.
bool(1)
# Note that other than 0 all numbers will return True
bool(2)
bool(0)
List to Tuple.
cust_id = [25,26,27,28,29]
type(cust_id)
cust_id_tuple = tuple(cust_id)
type(cust_id_tuple)
cust_id_tuple
List to Set.
cust_id = [25,26,27,28,29]
type(cust_id)
cust_id_set = set(cust_id)
type(cust_id_set)
cust_id_set
Set to Tuple.
cust_id = {25,26,27,28,29}
type(cust_id)
cust_id_tuple = tuple(cust_id)
type(cust_id_tuple)
cust_id_tuple
Set to List.
cust_id = {25,26,27,28,29}
type(cust_id)
cust_id_list = list(cust_id)
type(cust_id_list)
cust_id_list
Set to List.
cust_id = (25,26,27,28,29)
type(cust_id)
cust_id_list = list(cust_id)
type(cust_id_list)
cust_id_list
Tuple to Set.
cust_id = (25,26,27,28,29)
type(cust_id)
cust_id_set = set(cust_id)
type(cust_id_set)
cust_id_set