Learning Python Part-18: Python Data Type Conversion

The process of converting the value of one data type (integer, string, float) to another data type is called type conversion. Python has two types of type conversion.

  • Implicit Type Conversion
    • In Implicit type conversion, Python automatically converts one data type to another data type. 
    • This process doesn’t need any user involvement.
    • Python avoids the loss of data in Implicit Type Conversion.
    • Implicit Type conversion example:

    • Output:

  • Explicit Type Conversion (AKA Type casting)
    • In Type Casting loss of data may occur as we enforce the object to specific data type.
    • In Explicit Type Conversion, users convert the data type of an object to required data type.
    • We use the predefined functions like int(), float(), str() to perform explicit type conversion.
    • This type conversion is also called typecasting because the user casts (change) the data type of the objects.
    • Syntax : (required_datatype)(expression)
  • Explicit Type example:
    • Without type casting

    •  Result will be error as below


    • With type casting

    •  Output:


Leave a Reply