Python Introduction

Python Type Conversion and Type Casting (With Examples)

Type conversion is quite a familiar topic to all of us, as we have used many conversion techniques while solving different types of mathematical or scientific problems during our schooling period. For instance, converting a Celsius value to a Fahrenheit value or converting a binary number to a decimal number, and so on. Likewise, we will need to convert data types while programming. In python typecast technique is used to handle any conversions that might come your way. Read on to learn more! 

Type Conversion

In Python, the process of converting a data type value, be it an integer, string, float, etc, into another data type is known as type conversion. This type-conversion is of two types, which are: 

  1. Implicit Type Conversion 
  2. Explicit Type Conversion

Now, let us look into the details of these different types of type conversion methods and how they work. 

Implicit Type Conversion 

Implicit type conversion is where Python takes the burden of conversion from you and automatically converts one data type into another data type. This process doesn’t require any kind of involvement from you.

Python typecast

Source

Now, to get a clearer view, let us see an example where Python converts the lower data type – integer to float (higher data type) to avoid data loss. 

Example 1:

                    

int_num = 124
flo_num = 1.23
new_num = int_num + flo_num
print("datatype of int_num:",type(int_num))
print("datatype of flo_num:",type(flo_num))
print("Value of new_num:",new_num)
print("datatype of new_num:",type(new_num))

Output: The above code will result in the following output. 

                    

datatype of int_num: <class ‘int’>

datatype of flo_num: <class ‘float’>

Value of new_num: 125.23

datatype of new_num: <class ‘float’>

Here, we have added two numbers of which one is an integer data type while another is a float data type, but in the result, we can see that the added value is of float data type. It is because Python has converted the integer data type to float data type to avoid data loss.

Python typecast

Source

This is the conversion hierarchy followed by the code editors. So, during the Implicit Type conversion, Python automatically converts the lower data types into higher data as shown in the above image to avoid any data loss.

Example 2: 

Now, let us see how Python works and gives the output when it is instructed to add an integer (lower) data type and string (higher) data type.

                    

int_num = 125
str_num = "478"
print("Data type of int_num:",type(int_num))
print("Data type of str_num:",type(str_num))
print(int_num+str_num)

Output: The above code will result in the following output: 

                    

Data type of num_int: <class ‘int’> 

Data type of num_str: <class ‘str’> 

Traceback (most recent call last): 

  File “python”, line 6, in <module> 

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

Here, we can see Python has returned an error message when we tried to add an integer data type and string data type, as it can’t perform such type of operations using the Implicit Conversion method. Nevertheless, we can use the Explicit Conversion method to perform these types of operations. 

Now, let us look into the details of how Explicit Type Conversion works and how we can use it to perform conversion operations. 

Explicit Type Conversion 

In Explicit Type Conversion, Python allows you to convert an object’s data type to your required data type by using predefined functions such as float(), int(), str(), etc. These predefined functions help you to perform Explicit Type conversion and do various operations on different objects.  

This type of type conversion is also known as typecasting as the programmer casts (changes) the object’s data type. 

Syntax: <required_datatype>(expression)

We assign the required expression to a predefined data type function into which we want to convert the object’s data type. 

Example 3: 

Adding an integer data type and string data type using Explicit Type Conversion 

                    

int_num = 125
str_num = "478"

print("Data type of int_num:",type(int_num))
print("The data type of str_num before Explicit Type Conversion:",type(str_num))

str_num = int(str_num)
print("Data type of str_num after Explicit Type Conversion:",type(str_num))

Sum_ of_num = int_num + str_num

print("Sum of int_num and str_num:",sum_of_num)
print("The data type of the sum of numbers:",type(sum_of_num))

Output: The above code will result in the following output: 

                    

Data type of int_num: <class ‘int’>

The data type of str_num before Explicit Type Conversion: <class ‘str’>

Data type of str_num after Explicit Type Conversion: <class ‘int’>

Sum of int_num and str_num: 603

The data type of the sum of numbers: <class ‘int’>

Here, we can see that instead of throwing an error message Python has given the output by adding the two numbers and the data type as integer. It is because we have used the Explicit Type Conversion method and converted the string data type to integer data type before adding the two numbers using the predefined int() function. 

Key Points to Remember 

  • Type conversion is the process of converting an object’s data type from its original data type to our required data type. 
  • Implicit Type Conversion is carried out automatically by the Python Interpreter 
  • Python prevents data loss by performing Implicit Type Conversion 
  • Explicit Type conversion, also known as Typecasting, is used to convert an object’s data type by using some predefined Python functions. 
  • In Explicit Type Conversion, data loss may occur as we enforce the object to a required specific data type. 

Q & A

Now, to give you a brief idea of how type conversion and typecasting works in Python and how to use these methods, we have answered some frequently answered questions, which are: 

Q1) What is a typecast in Python?

A. In Python, typecast is nothing but Explicit Type Conversion, where we use predefined Python functions such as float(), str(), int(), etc, to convert an object’s data type from its original data type to our required data type. 

Syntax for the typecast in Python is: <required_datatype>(expression)

Q2) What are all the various typecasting possible in python?

A. In Python, typecasting can be performed on various data types allowing us to convert an object from one data type into another data type. The various possible typecasting in Python is:

  • Int(): Using this int() function which takes string or float as an argument, you can convert an object’s data type into an integer data type. 
  • float(): Using this float() function which takes string or integer as an argument, you can convert an object’s data type into a float data type. 
  • str(): Using this str() function which takes integer or float as an argument, you can convert an object’s data type into a string data type.

Q3) How do you convert type in Python?

A. In Python, we can convert the data type of an object by using type conversion methods. Type Conversion is the process of converting a data type value, be it an integer, string, float, etc, into another data type. This type-conversion is of two types, which are: 

  • Implicit Type Conversion: No need for the programmer’s involvement as the Python interpreter automatically converts the data type. 
  • Explicit Type Conversion: Programmers can change an object’s data type to a specific data type using predefined Python functions such as float(), str(), int(), etc. 

Q4) How do you cast an object in Python?

A. In Python, you can cast (change) an object’s data type using the Explicit Type conversion (also known as typecasting) method. Typecasting allows you to use predefined Python functions such as int(), string(), float(), etc, and convert the object’s data type from its original data type to your required specific data type. 

Syntax for the typecast in Python is: <required_datatype>(expression)

Share with friends

Customize your course in 30 seconds

Which class are you in?
5th
6th
7th
8th
9th
10th
11th
12th
Get ready for all-new Live Classes!
Now learn Live with India's best teachers. Join courses with the best schedule and enjoy fun and interactive classes.
tutor
tutor
Ashhar Firdausi
IIT Roorkee
Biology
tutor
tutor
Dr. Nazma Shaik
VTU
Chemistry
tutor
tutor
Gaurav Tiwari
APJAKTU
Physics
Get Started

Leave a Reply

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

Download the App

Watch lectures, practise questions and take tests on the go.

Customize your course in 30 seconds

No thanks.