At Agira, Technology Simplified, Innovation Delivered, and Empowering Business is what we are passionate about. We always strive to build solutions that boost your productivity.

,

How To Convert Data Types in Python 3

  • By Aishwarya Damani
  • October 9, 2019
  • 3613 Views

Before going to Data type conversion in Python procedure, you should have some basic knowledge of data types. Some of the most common data types in Python are numeric, string and boolean types. There are also built-in data types in Python that are dict, list, set and frozenset, and tuple.

Data types in PythonWhat is data type conversion in Python?

As the name says, it is converting the value of one data type to another. This process is called data type conversion in Python.
How many types of conversion is there in Python?
Similar to most languages, there are two types of conversion.

  1. Implicit Type Conversion
  2. Explicit Type Conversion

What is Implicit Type Conversion?

In implicit conversion, Python converts one data type into another data type without any user involvement.
Let’s consider the following code.

int_number = 843
float_number = 8.43
result= int_number + float_number
print("Data Type of Integer:",type(int_number))
print("Data Type of Float:",type(float_number))
print("Result Value:",result)
print("Data Type of Result:",type(result))

Output:

('Data Type of Integer:', <type 'int'>)
('Data Type of Float:', <type 'float'>)
('Result Value:', 851.43)
('Data Type of Result:', <type 'float'>)

Note that there is no requirement for any conversion procedure here because Python will handle these kinds of conversions automatically.

What is Explicit Type Conversion?

In Explicit conversion, the user has to convert the data type of an object to the desired data type as per requirement. One can achieve the above using the default functions – float(), int(), string().
This process is sometimes called typecasting of data, as the user casts the data type of any object.
Consider the code below.

int_number = 843
string_number = “843”
print("Data Type of Integer:",type(int_number))
print("Data Type of String:",type(string_number))
print(int_number + string_number)

Its output will throw the following error.

Data Type of Integer: <class 'int'>
Data Type of String: <class 'str'>
Traceback (most recent call last):
File "<stdin>", line 6, in <module>
print(int_number+string_number)
TypeError: unsupported operand type(s) for +: 'int' and 'str'

In this scenario, Python cannot handle data type conversion automatically. So, we use Explicit conversion for these scenarios.
But how?
Check this code out!

int_number = 843
string_number = “843”
print("Data Type of Integer:",type(int_number ))
print("Data Type of String before casting:",type(string_number ))
string_number = int(string_number )
print("Data Type of String after casting:",type(string_number ))
result = int_number + string_number
print("Result:",result )
print("Data type of result :",type(result ))

Successful output:

Data Type of Integer: <class 'int'>
Data Type of String before casting: <class 'str'>
Data Type of String after casting: <class 'int'>
Result: 1686
Data type of result: <class 'int'>

In the above example, Python cannot convert data types automatically when you try to add two different data types that are not possible. To overcome such scenarios where Python cannot use implicit conversion, we use explicit conversion.

Here are some of the key points that you need to keep in mind when converting data types.

  1. Conversion always happens from one data type object to another data type object.
  2. Python interpreter will take care of Implicit conversion automatically, also, it avoids loss of data.
  3. Explicit Conversion also called Casting, when in-built functions were being used to perform this action.
  4. In Explicit Conversion, if we forcibly convert to a specific data type, there are chances for loss of data.

That’s all about conversion. If you have further questions or queries, write it in the comments.

Hire the best Python developers for your web development projects easily with Agira technologies. We provide you with dedicated experts who have mastered the latest technologies. Endure the fullest of outsourcing tech experts for your company. Get in touch with our experts to hire freelance python developers, offshore python developers and python web developers who can cater to your company’s requirement.

Turn your vision to magnificent reality with
Our Web and Mobile Solutions

Read more blogs related to Python such as 7 Reasons Why Python Beats PHP for Web Development and How to Hire a talented Python developer for your business. Get regular updates by subscribing to us.

Aishwarya Damani

An aspiring adolescent started her career as Software Engineer Trainee and slowly she's mastering all the rising technologies like Nodejs, Express.js, React.js, MongoDB, Sails. On the other hand, a perfect decision maker who is passionate about coding. Wait! there's half a lie! Yeah, this crazy foodie has an intense long lasting affair on trying different foods & exploring places.