In Python, Type Casting, also called Type conversion, is a method of converting the variable’s data type into another in order to perform certain operations. Python being a dynamically typed language does not allow us to manually declare the types of the variables. Python automatically determine it during the runtime.
Let us take a look at the following example:
Example
# initializing variables
num_1 = 12 # int
num_2 = 3.6 # float
res = num_1 + num_2 # resultant value is a float
print(res, "=>", type(res))
Output:
15.6 => <class 'float'>
Explanation:
Here, Python has automatically determined the data type of the resultant variable.
However, Python also provides various built-in functions, such as int(), float(), and str(), to explicitly perform these type conversions when necessary.
Types of Python Type Casting
Type casting in Python is classified into two types:
- Implicit Type Casting – The Python interpreter automatically converts one data type into another without user intervention.
- Explicit Type Casting – The user manually converts a variable’s data type by using built-in capabilities.

Implicit Type Casting in Python
Implicit Type Casting, or Type Coercion, is when Python automatically changes one data type to another during runtime without needing the programmer’s input.
Python follows a hierarchy of data types to ensure safe implicit conversions. The hierarchy is as follows:
- An integer may be transformed into a floating-point or a complex number. However, a complex number cannot be converted back implicitly.
- Python avoids downgrading types implicitly (e.g., floating-point number to integer), as it can lead to precision loss.
Let us see the following example of implicit type casting in Python.
Example
# simple program to show implicit type casting
# initializing variables
a = 5 # int
b = 7.6 # float
c = 3 + 4j # complex
# implicit type casting
d = a + b # implicit conversion: int -> float
e = a + b + c # implicit conversion: int -> complex
print(d, '=>', type(d)) # returns float
print(e, '=>', type(e)) # returns complex
Output:
12.6 => <class 'float'>
(15.6+4j) => <class 'complex'>
Explanation:
In the above example, we have initialized several variables of different types. However, Python changed their data types automatically during runtime so that the operations can be carried out properly.
Explicit Type Casting in Python
Explicit Type Casting, also known as Type Conversion, is when the programmer manually changes the type of a variable or value by calling Python’s built-in function such as int(), float(), str(), and more.

Functions of Python Type Casting
Python provides several built-in functions to perform explicit type casting:
| Function | Description |
|---|---|
| int() | This function converts specified variable or value to an integer (truncates decimals if x is a float). |
| float() | This function is used to convert specified variable or value to a floating-point number. |
| complex() | This function converts specified variable or value to a complex number. |
| str() | This function is utilized to convert specified variable or value to a string. |
| bool() | This function converts given variable or value to a Boolean value (True or False) |
| list() | This function is used to convert the given iterable (like string, tuple, set) to a list. |
| tuple() | This function converts the given iterable (list, string, etc.) to a tuple. |
| set() | This function is used to convert the given iterable (list, tuple, etc.) to a set. |
| dict() | This function is used to convert the given sequence of the key-value pairs to a dictionary. |
Let us discuss some of these functions with the help of examples.
Integer Conversion (int())
The int() function is used to convert the variable or value to an integer. This function truncates the decimals if the given variable is float.
Let us see the following example:
Example
# python program to convert variables and values to integers
# explicit type casting
int_1 = int(16.2) # float -> int
int_2 = int('14') # str -> int
# printing results
print(int_1, "->", type(int_1)) # decimal part is removed
print(int_2, "->", type(int_2))
Output:
16 -> <class 'int'>
14 -> <class 'int'>
Explanation:
Here, we have used the int() function to convert the given data type into the int data type.
Floating-point Number Conversion (float())
The float() function is used to convert the variable or value to a floating-point number.
The following example shows the working of Python float() function.
Example
# python program to convert variables and values to floating-point numbers
# explicit type casting
float_1 = float(19) # int -> float
float_2 = float('21.73') # str -> float
# printing results
print(float_1, "->", type(float_1)) # .0 is added as suffix
print(float_2, "->", type(float_2))
Output:
19.0 -> <class 'float'>
21.73 -> <class 'float'>
Explanation:
Here, we have used the float() function to convert the given data type into the float data type.
Complex Number Conversion (complex())
The complex() function is used to convert the variable or value to a complex number. Generally, it converts the specified integer or float, x into x + 0j.
Moreover, we can specify the real and imaginary parts, x and y as the arguments of this function to return a complex number as x + yj.
Let us see working of the complex() function with the help of the following example.
Example
# python program to convert variables and values to complex numbers
# explicit type casting
complex_1 = complex(5) # int -> complex
complex_2 = complex(8.9) # float -> complex
# printing results
print(complex_1, "->", type(complex_1)) # 0j is added
print(complex_2, "->", type(complex_2))
Output:
(5+0j) -> <class 'complex'>
(8.9+0j) -> <class 'complex'>
Explanation:
Here, we have used the complex() function to convert the given data type into the complex data type.
String Conversion (str())
The str() function is used to convert the variable or value to a string.
Let us take a look at the following example showing the usage of the str() function.
Example
# python program to convert variables and values to strings
# explicit type casting
str_1 = str(5) # int -> str
str_2 = str(8.9) # float -> str
str_3 = str(5 + 9j) # str -> str
str_4 = str(True) # bool -> str
# printing results
print(str_1, "->", type(str_1))
print(str_2, "->", type(str_2))
print(str_3, "->", type(str_3))
print(str_4, "->", type(str_4))
Output:
5 -> <class 'str'>
8.9 -> <class 'str'>
(5+9j) -> <class 'str'>
True -> <class 'str'>
Explanation:
Here, we have used the str() function to convert the given data type into the string data type.
Boolean Conversion (bool())
bool() is the built-in function in Python that allow us to convert a specified variable or value to a Boolean (True or False).
Let us see the following example:
Example
# python program to convert data type to Boolean
# explicit type casting
bool_1 = bool(0) # int -> bool
bool_2 = bool('') # str -> bool
bool_3 = bool('Tpoint Tech') # str -> bool
bool_4 = bool(14.5) # float -> bool
bool_5 = bool([]) # list -> bool
bool_6 = bool((1, 3, 5)) # tuple -> bool
# printing results
print(bool_1, "->", type(bool_1))
print(bool_2, "->", type(bool_2))
print(bool_3, "->", type(bool_3))
print(bool_4, "->", type(bool_4))
print(bool_5, "->", type(bool_3))
print(bool_6, "->", type(bool_4))
Output:
False -> <class 'bool'>
False -> <class 'bool'>
True -> <class 'bool'>
True -> <class 'bool'>
False -> <class 'bool'>
True -> <class 'bool'>
Leave a Reply