In this article, you will learn how to convert data types in Python with in-built methods. Before we get started, if you want to Data Type in Python, please go through the following article: Data Types in Python.

Every variable in Python has a Datatype. Although you don’t declare them while using them declarations happen automatically when you assign a value to the variable. These datatypes play an essential role in programming because they determine what kind of operation can be performed on the value.

Implicit and Explicit Data Type Conversion

Data type conversion in Python can take place in two ways, either you force Python to convert it to a specific datatype or Python itself does that during compilation.

When you force the compiler for conversion, it is called Explicit Data Type Conversion and when Python itself does the work, it’s called implicit Data Type conversion. A good example of implicit data type conversion can be seen by performing a simple division,

We did an operation on two integers and stored the result into another variable Python compiler itself converted the result to a float value to prevent Data loss, and this is a quite effective feature of Python. Another example of implicit data type conversion can be,

In this example, we added an integer with a float, and the result automatically gets converted into a float. All these Implicit conversions are the result of the Python compiler defense mechanism which is firmly against data loss. Now, let’s have a look at Explicit conversions.

Converting Number Types

Python has three number types – integer, float, and complex. Although you will mostly use Integer and float numbers in your program. Python comes with inbuilt methods to convert an integer to float and float to an integer.

Convert Data Type: Integers to Floats

The  float() method in Python converts integers to floats. We can simply pass the integer itself or a variable storing integer inside the parenthesis, Like this

Convert Data Type: Floats to Integers

The  Int() method is used to convert floats into integers,

However, you might have noticed Python doesn’t return a round figure integer, it simply cuts off the decimal part.

Convert Data Type: Numbers to Strings

A string in Python is a sequence of alphanumeric characters wrapped inside single or double quotes. The  str() method is used to convert numbers either be integer or float to a string.

Quotation marks around the number indicated that it is no longer a number. A better real-world example would be:

We need to have the items in numbers to perform addition, but we must convert total to a string to concatenate with the final string object.

Converting Strings to Numbers

Strings can be converted into numbers by either  int() or  float() method. However, keep in mind that  int() method won’t convert a string which has a decimal number in it, use  float() method in such condition.

Till now we were converting primitive data types which are the basic building blocks of any data structure now let’s learn how to convert non-primitive data structures like lists and tuple.

Conversion of Non Primitive datatypes

List and tuple both are used to group data of similar or different data types. Tuples are ordered sequences of items contained within parenthesis(). Tuples are immutable, which means the items of tuples cannot be updated or deleted.

Similar to tuples, Lists are ordered sequence of items contained in [], but they are mutable which meanss they can be updated, deleted or a new item can be inserted into the existing list.

Converting Lists into Tuple

Tuples are immutable, converting lists into tuple can provide a significant performance boost to the existing program, you can do this by Python’s  tuple() method.

The parenthesis() indicates that it is no longer a list.

Converting Tuples into Lists

Lists are mutable, converting lists into tuple gave you the ability to add, delete and update elements. The  list() method is used to convert tuples into list.

The square brackets indicate that the operation returns a list.

Convert String into tuples and Lists

A string can be converted into List and Tuple through their respective conversion method.

Notice the brackets they confirm that the conversion was successful.

How To Convert Data Types in Python

Leave a Comment