In this tutorial, we will learn about Python Type Casting by using python type conversion.
To understand python typecasting, we need a basic understanding of data types.

What is Python Type Casting and Type Conversion?
Python Casting or typecasting is the way to change the data type of a variable, which can help day-to-day competitive programming.
In python, there are two types of Type conversion:
- Implicit Type Conversion
- Explicit Type Conversion
Let us understand python typecast one by one.
Implicit Type Conversion
When the python interpreter automatically changes the data type without programmer intervention, it is called Implicit type conversion. Object’s data type.
Let see a simple example of implicit type conversion in python.
Example:
When we execute the above program, we will get the following result.
Output:
Here we are taking the X variable as Integer and the Y variable as a float. Then we are adding Y’s value, which is a float with Value of X, which is Integer when after the addition, we are getting output as 5.5 in a float. We can also see that after the addition, the data type of X is also changed to float from an integer automatically by the python interpreter; this is how Implicit type conversion works.
This type of conversion is also called UpCasting.
Explicit Type Conversion
Unlike Implicit type conversion, in explicit type conversion, we have to manually convert the data type of an object Object’s data type.
We can use the following predefined functions to perform explicit type conversion.
- int()
- float()
- str()
- bool()
- complex()
- list()
- set()
- tuple()
- dict()
- chr()
- ord()
- hex()
- oct()
Python int()
The int() function converts the base of any number or a string to an integer.
Let us check an example of the int() function.
Example:
The output will be as follows.
In the above program,
We are declaring variable X using the int() function. Then we are declaring variable Y with a float. Then we convert it using the int() method from float to integer.
We are declaring variable Z with string, and then we are converting it to an integer.
We convert the Boolean value to an integer which refers to True as 1 and false as 0.
Note:
- We can only convert string if an only string has a base with 0-9. We cannot convert a string like A-Z to the integer using int()
- We cannot convert complex datatypes to an integer.
Python float()
This function is used to convert any data types of a float number.
For Example
When we run the above code, we will get the following output:
Note:
- We can only convert string if the only string has a base with 0-9. We cannot convert a string like A-Z to the integer using float()
- We cannot convert complex data types to float.
Python str()
This function is used to convert data types like integer or float into a string.1
The output will be as follows.
Python bool()
This function is used to convert any data type to a boolean data type easily.
Example:
Output:
With the help of the bool function, we can convert any type of data type into a boolean. The output will be – For all values, it will produce True except for an empty String.
Python complex()
This function is used to convert natural numbers to complex numbers. However, If we want to convert X and Y into complex numbers, X will be the real part, and Y will be the imaginary part.
Example:
The output will be as follows.
Python list()
This function is used to convert any data type into a list type.
Example:
Output:
Python set()
This function is used to convert any datatypes to a set.
Example:
The output will be as follows.
Python tuple()
This function is used to convert data types to a tuple.
Example:
The output will be as follows.
Python dict()
This function is used to convert a tuple with key and value of order into a dictionary.
Example:
The output will be as follows.
Python chr()
This function is used to convert numbers to their corresponding ASCII character.
The output will be as follows.
Python ord()
This function is used to convert a character to an integer.
Output:
Python hex()
This function is used to convert integers to hexadecimal strings.
Example:
Output:
Python oct()
This function is used to convert integers to octal strings.
Example:
Output: