Python Type Conversion And Type Casting



Type Conversion

Converting the value of one data type to another data type is called type conversion or typecasting.

Python has two different types of type conversion:

  • Implicit Type Conversion
  • Explicit Type Conversion

Implicit Type Conversion

The implicit type conversion is when Python automatically converts one data type to another data type without any user involvement.


Converting Integer to Float - Example

In the following example, Python will make an implicit conversion from an integer data type to a float data type to avoid data loss.

n_int = 657
n_float = 36.12

n_new = n_int + n_float

print("datatype od n_int : ", type(n_int))
print("datatype od n_float : ", type(n_float))

print("value of n_new : ", n_new)
print("datatype of n_new : ", type(n_new))

After executing the above code, the output will be as follows:

datatype od n_int :  <class 'int'>
datatype od n_float :  <class 'float'>
value of n_new :  693.12
datatype of n_new :  <class 'float'>

As we can see in the above output, after adding an integer variable to a float variable, the result of this addition is a float type. Because Python always converts smaller data types (here Integer) to larger data types (here Float) to avoid data loss.


Addintion of integer (lower) data type and string (higher) data type - Example

In the following example, we will try an addition between an integer and a string and see how Python will deal with it.

n_int = 123
n_str = "456"

print("data type of n_int : ", type(n_int))
print("data type of n_str : ", type(n_str))

print(n_int + n_str)

After executing the above code, the output will be as follows:

data type of n_int :  <class 'int'>
data type of n_str :  <class 'str'>

TypeError Traceback (most recent call last)

----> 7 print(n_int + n_str)

TypeError: unsupported operand type(s) for +: 'int' and 'str'

As we can see in the above output, we got a TypeError because Python cannot use implicit conversion to do addition between string and integer values. However, we can found a solution for these types of situations using Explicit Type Conversion.


Explicit Type Conversion

The Explicit Type Conversion or Typecasting is when a user converts the data type of an object to another data type. You can use predefined functions like str(), int(), float(), etc to perform explicit type conversion.

Typecasting can be done by assigning the required data type function to the expression.


Addition of integer data type and string data type using explicit conversion - Example

In the following example, we will add an integer and a string using explicit typecasting.

n_int = 111
n_str = "222"

print("data type of n_int : ", type(n_int))
print("data type of n_str : ", type(n_str))

# explicit casting str to int
n_str = int(n_str)
print("data type of n_str after type casting : ", type(n_str))

n_sum = n_int + n_str

print("sum of n_int and n_str : ", n_sum)
print("data type of the n_sum is : ", type(n_sum))

After the execution of the above code, the output will be as follows:

data type of n_int :  <class 'int'>
data type of n_str :  <class 'str'>
data type of n_str after type casting :  <class 'int'>
sum of n_int and n_str :  333
data type of the n_sum is :  <class 'int'>

As you can see in the above output, after converting a string to int, Python can make the addition.


Summary

This chapter took a deep look at the two types of type conversion: Implicit Type Conversion and Explicit Type Conversion. The most important takeaways are:

  • Type Conversion is the conversion from one data type to another data type.
  • Implicit Type Conversion is automatically done by Python.
  • Python avoids the data in Implicit Type Conversion.
  • Explicit Type Conversion is also known as Type Casting.
  • In Type Casting, the data types are converted using predefined functions by the user.
  • In Type Casting, loss of data may occur as the user enforce the conversion between data types.


ExpectoCode is optimized for learning. Tutorials and examples are constantly reviewed to avoid errors, but we cannot warrant full correctness of all content. While using this site, you agree to have read and accepted our terms of use, cookie and privacy policy.
Copyright 2020-2021 by ExpectoCode. All Rights Reserved.