Python Type Conversion

This lesson will teach you about Type Conversion in Python and how to use it. Before learning about Type Conversion in Python, you need to know what Python Data Types are, which were covered in the previous lesson.


Type Conversion in Python

Type conversion is changing a value from one data type (integer, string, float, etc.) to another. Python has two ways to change types.

  • Python Implicit Type Conversion
  • Python Explicit Type Conversion

Python Implicit Type Conversion

When implicit type conversion is on, Python automatically changes from one data type to another. The user has nothing to do with this process.

Let’s look at how Python prevents data loss by converting the lower data type (integer) to the higher data type (float).

Example

intVar = 99
floatVar = 99.99
Sum = intVar + floatVar
print("intVar datatype:",type(intVar))
print("floatVar datatype:",type(floatVar))
print("Sum:",Sum)
print("Sum DataType:",type(Sum))

Output

intVar datatype: 
floatVar datatype: 
Sum: 198.99
Sum DataType: 

In the show above,

  • We add two variables, intVar, and floatVar, and store their values in Sum.
  • We will look at the type of data each of the three objects has.
  • In the output, we can see that intVar is a type integer and floatVar is a type float.
  • Also, we can see that Sum has a float data type because Python always changes smaller data types to larger ones to avoid losing data.

Now, let’s try adding two variables, a string, and an integer, to see how Python handles adding a string (higher data type) and an integer (lower data type).

Example

intVar = 99
stringVar = "99"
print("Data type intVar:",type(intVar))
print("Data type stringVar:",type(stringVar))
Sum = intVar + stringVar
print("Sum:",Sum)
print("Sum DataType:",type(Sum))

Output

Data type intVar: 
Data type stringVar: 
Traceback (most recent call last):
File "", line 6, in 
TypeError: unsupported operand type(s) for +: 'int' and 'str'

In the show above,

We add intVar and stringVar, which are both variables. The output shows that we got TypeError, and Python can’t use Implicit Conversion in these situations. But Python has a way of dealing with these problems called “Explicit Conversion.”


Python Explicit Type Conversion

In Explicit Type Conversion, the data type of an object is changed to the type of data needed. For explicit type conversion, we use built-in functions like int(), float(), str(), and so on. This kind of conversion is also called “typecasting” because the user changes the data type of the objects.

Basic Syntax

(expression)

Typecasting can be done by giving the expression the function for the data type it needs.

Example

intVar = 99
stringVar = "99"
print("Data type intVar:",type(intVar))
print("Data type stringVar:",type(stringVar))
Sum = intVar + int(stringVar)
print("Sum:",Sum)
print("Sum DataType:",type(Sum))

Output

Data type intVar: 
Data type stringVar: 
Sum: 198
Sum DataType: 

We run the same example used in implicit conversion but after typecasting.

  • We add two new variables: stringVar and intVar.
  • Python successfully performed addition by changing the type of num str from a string (higher) to an integer (lower) using the int() function.
  • Python can add these two variables once the string has been turned into an integer value.
  • We ensured that the value and data type of both numbers were integers.

Important Things to Remember

  • Type conversion is changing an object from one type of data to another.
  • The Python interpreter takes care of implicit type conversion by itself.
  • In Implicit Type Conversion, Python keeps data from being lost.
  • Type Casting is another name for Explicit Type Conversion. The user changes the data types of objects by using functions that have already been set up.
  • In Type Casting, we may lose data when we force an object to be a certain type of data.

This concludes the Python Type Conversion lesson. In the next lesson, you will learn Keywords and Identifiers in Python