Python Type Conversion | How to do type conversion in Python

type conversion


In this article, we understand the type conversion with the example.

Pre knowledge is required to learn type conversion, so make sure you know the type of data types in python.


What is Type Conversion?

Process of the converting one data type to another data type is called Type Conversion.
Python has two Type Conversion:
  1. Implicit Type Conversion
  2. Explicit Type Conversion


Implicit Type Conversion

Implicit Type Conversion is the conversion where python automatically converts one data type to another data type. This process is automatically done by python, with no need of any user involvement. 

Let's see an example where python converts the integer data type to float data type.

Example:

num1 = 10 # integer
num2 = 10.5 # float

#this can convert the integer to float => 10+10.5= 20.5
num3 = num1 + num2

# Print both the data types
print("Datatypes of num1: ",type(num1))
print("Datatypes of num2: ",type(num2))

# Now result data types
print("Datatypes of num3: ",type(num3))

Output:

Datatypes of num1:  <class 'int'>
Datatypes of num2:  <class 'float'>
Datatypes of num3:  <class 'float'>

Explanation of the above program:

  1. In the above program first, we take two-variable num1 and num2.
  2. Where num1 we store 10 and num2 we store 10.5
  3. Now we make another variable num3 and store the value of num1+num2
  4. And then we print the data types of all the variables, num1, num2 and num3
  5. num3 converted in the float because python always converts the lower data types to higher data types because to prevent the loss of data.


Explicit Type Conversion

In Explicit Type Conversion, the user converts one data type to another using int(), str(), and float() function, this function is already predefined. 

This conversion is also know as TypeCasting converison.

Example:

num1 = 10 # integer
num2 = "12" # strings

print("Before typecasting")
# Print both the data types
print("Datatypes of num1: ",type(num1))
print("Datatypes of num2: ",type(num2))

# convert string to int
new_num = int(num2)

print("After typecasting")
# Now result data types
print("Datatypes of num1: ",type(num1))
print("Datatypes of num2: ",type(new_num))

Output:

Before typecasting
Datatypes of num1:  <class 'int'>
Datatypes of num2:  <class 'str'>
After typecasting
Datatypes of num1:  <class 'int'>
Datatypes of num2:  <class 'int'>

Explanation of the above program:

First, we take two number first is in integer form and another is in strings form.
Now, we print the type of datatype in both num1 and num2.
Now, we convert the num2 into the integer using int() function. 
And then again print the type of data types is new_num.

Next Post Previous Post
1 Comments
  • SEO Services
    SEO Services August 10, 2021 at 3:23 PM

    Great article with excellent idea!Thank you for such a valuable article. I really appreciate for this great information.. convert pdf to docx

Add Comment
comment url