Python Type Conversion & Type Casting

By | November 1, 2020
Python Type Conversion & Type Casting

In this tutorial, we will discuss the Type conversion and what are its uses but before that make sure that you have the complete knowledge of Python Data types that we have coved on the previous tutorial. so let’s discuss Python Type Conversion & Type Casting.

Python Type Conversion & Type Casting


Type Conversion:

As in the previous tutorials we have mentioned that python is a Dynamic-type language that means you do not need to specify the type of data which you are assigning to a variable the python interpreter will itself understand the type of Variable.

But in python, you get this feature by which you can convert the type of Data-type from one to another that’s what Type conversion is all about. Type conversion is a method or process of converting one data type to another. That could be any Data-Type an integer or a Float or a String or List or Dictionary any data-type.

There Are two types of Type-Conversion in python.

  1. Implicit Type Conversion
  2. Explicit Type conversion

Implicit Type Conversion

When the python itself convert the data-type of the variable that is known as implicit data conversion.

Let’s understand it with an example suppose that you take two variable var_int and var2_float where var_int is an integer data type and var_float is a float data type, but when you apply division or subtraction or multiplication or addition operation between them, you will get a Float because of the Implicit type conversion of python.


var_int =10                                          #it is an integer
var_float=10.0                                   #it is a Float
print("the type of var_int is", type(var_int))
print("the type of var_float is ", type(var_float))
add_vars = var_float + var_int                    #new data type add_vars will be float not int
print("the value of add_vars is ", add_vars, "and its type is ",type(add_vars))


the type of var_int is <class ‘int’>
the type of var_float is  <class ‘float’>
the vaue of add_vars is  20.0 and its type is  <class ‘float’>

What if we Apply + operator between an integer and string?

If we apply a + operator between an integer and string we will get a Type error.

let’s understand it with an example.

var_int = 69
var_string = "69"                               # it is an string because 69 is between the " "
print("the data type of var_int is ", type(var_int))
print("the data type of var_string is ", type(var_string))
print( var_string + var_int)           #here we are adding and a string 69 with an integer 69


the data type of var_int is  <class 'int'>
the data type of var_string is  <class 'str'>
Traceback (most recent call last):
File "", line 7, in <module>
print( var_string + var_int)
TypeError: can only concatenate str (not "int") to str

Explicit Type Conversion:

There are pre-defined functions in the python such as str(), int(), float(), list(), dict(), set(), and tuple() which are used to convert one data to another. In Explicit Data type conversion you get some functions by which you can convert the Data type of variables.

Explicit conversion of Data-types with examples:

var_int = 144
var_float = 144.0
var_str_int = "69"
print("type of var_int is ", type(var_int))
print("type of var_float is ", type(var_float))
print("type of var_str_int is ", type(var_str_int))

#Explicit type Conversion

print("var_int converted into string ", str(var_int) )
print("var_int converted into Float ", float(var_int))
print("var_float converted into integer", int(var_float))
print("var_float converted into str", str(var_float))
print("var_str_int converted into integer", int(var_str_int))
print("var_str_int converted into float", float(var_str_int))
print("var_str_int converted into list", list(var_str_int))
type of var_int is  <class 'int'>
type of var_float is  <class 'float'>
type of var_str_int is  <class 'str'>
var_int converted into string  144
var_int converted into Float  144.0
var_float converted into integer 144
var_float converted into str 144.0
var_str_int converted into integer 69
var_str_int converted into float 69.0
var_str_int converted into list ['6', '9']

Points to Remember:

  • In type Conversion, you can convert a Data type to another data type
  • In Explicit Type conversion, you can only convert those string to an integer or Float which contain only digits.
  • Explicit Type conversion is also known as type casting

Python Tutorials:


Flow Control



File Handling

Object & Class

Additional Tutorials

Leave a Reply

Your email address will not be published. Required fields are marked *