The Process of converting the value of one data type (integer, string, float, etc.) to another data type is called type conversion. In Python, type conversion can be classified into two types.
- Implicit Type Conversion
- Explicit Type Conversion
Implicit Type Conversion
In Implicit type conversion, Python automatically converts one data type to another data type without any user involvement.
Example:
#Converting integer to float
myint= 314
myfloat = 3.14
mynum = myint + myfloat
printf(type(myint))
printf(type(myfloat))
print(mynum)
print(type(mynum))
Explicit Type Conversion
In Explicit Type Conversion, the users themselves convert the data type of an object to another data type, which they require. Predefined functions, like int(), float(), str(), etc…, are used to perform explicit type conversion.
Example
#Addition of string and integer using explicit conversion
myint = 299
mystr = "459"
print(type(myint))
print(type(mystr))
newstr= int(mystr) #explicitly conversion string to integer
print(type(mystr))
mysum = myint + newstr
print(mysum)
print(type(mysum))