Type casting in Python
Type Casting is the method to convert the Python variable data type into a certain data type in order to perform the required operation by users. In this article, we will see the various techniques for typecasting.
There can be two types of Type Casting in Python:
- Python Implicit Type Conversion
Implicit Type Conversion in Python
In this, method, Python converts the data type into another data type automatically. Users don’t have to involve in this process.
Example of implicit type conversion
integer_number = 123
float_number = 1.23
new_number = integer_number + float_number
# display new value and resulting data type
print(“Value:”,new_number)
print(“Data Type:”,type(new_number))
Output
Value: 124.23
Data Type: <class ‘float’>
Explicit Type Conversion
In Explicit Type Conversion, users convert the data type of an object to required data type.
We use the built-in functions like int(), float(), str(), etc to perform explicit type conversion.
This type of conversion is also called typecasting because the user casts (changes) the data type of the objects.
Example of Explicit type conversion
num_string = ’12’
num_integer = 23
print(“Data type of num_string before Type Casting:”,type(num_string))
# explicit type conversion
num_string = int(num_string)
print(“Data type of num_string after Type Casting:”,type(num_string))
num_sum = num_integer + num_string
print(“Sum:”,num_sum)
print(“Data type of num_sum:”,type(num_sum))
Output
Data type of num_string before Type Casting: <class ‘str’>
Data type of num_string after Type Casting: <class ‘int’>
Sum: 35
Data type of num_sum: <class ‘int’>