Type Conversion in Python

type conversion is converting from one data type to another. Python has inbuilt functions to convert from one data type to another. Type conversion can be:
  • Implicit Type conversion
  • Explicit Type conversion
Implicit type conversion is taken care by the Python interpreter where user need not use any function to specifically convert types. Example as below:

a = 5
b = 5.5
a = a + b
print(a)

The above code prints 10.5

Here a is integer data type initially and then it is converted to float type.

Explicit type conversion or type casting is converting data types using the inbuilt functions of Python. Some of the inbuilt functions are,

float()
converts any input to this function to float data type

ord()
converts character to integer

str()
used to convert integer to string

chr()
converts number input to corresponding ASCII

Post a Comment

0 Comments