Programming languages have a defined set of do’s and don’t. The compiler will force you to follow these before you can run your code. In a way, that is good for the security and smooth functioning of the software but sometimes a reason for frustration and several coffee cups for the developer. One such thing is the data types in Python. Data types in a programming language such as python are fixed and follow certain rules. Python does not compromise with them and hence we perform typecasting. Sometimes, as we will see, Python does that by itself! Not getting ahead of myself, let me introduce the key highlights of this post with you. In this post, Type Conversion and Type Casting in Python, we shall get familiar with the following:
- Type Casting in Python
- Implicit Type Conversion in Python
- Explicit Type Conversion in Python (Type Casting in Python)
Note: Please go through the article Data types in Python to understand this article in a better way.
Type Casting In Python
Typecasting in Python is a process of changing one data type into another. A lot of the time, while dealing with certain operations, we need to change the data type to achieve successful results. For example, if we want to concatenate an int variable to a string such as (“My age is ” + age ), it will result in errors since one data type is a string and one is int. Both of the data types use different approaches with the “+ ” sign. The integer will add another integer while the string will concatenate with another string. Therefore, we need to explicitly tell Python that apply the cast to the data type.
Hence it is called an explicit type conversion in Python with which we will deal later in the post. For now, just remember that the process of changing a data type to another is called type conversion. It is of two types:
- Implicit Type Conversion
- Explicit Type Conversion
We will see them one by one with a few examples.
Implicit Type Conversion In Python
Implicit type conversion means that Python will implicitly know that the data type needs to be changed. The interpreter does not need any explicit command from the user and wherever it makes sense, it will perform the type conversion in Python. For example, 0 can be converted to boolean False, etc.
As an example, in the below-given code, I am initializing an int variable, a float variable, and adding them up. The “add ” variable will help you understand how implicit type conversion works in Python.
1 2 3 4 5 6 |
a = 2 print(type(a)) b = 3.0 print(type(b)) add = a + b print(type(add)) |
Execute the above code to check all the three data types:
As noticed from the output, Python has automatically changed the data type of int without any external help to float and added them up.
Explicit Type Conversion in Python (Type Casting in Python)
The other type of type conversion is called an explicit type conversion in Python because we apply a cast externally to the data type.
As an example, if I have two variables:
1 2 3 |
age = 60 string = "My age is" |
Can I print “My age is 60 ” by the same conversion I did above in the implicit type conversion? Let’s see!
1 |
print(string + age) |
The following output is achieved:
Python indicates that it can only concatenate str to str. As I mentioned, Python has got its own rules that you need to follow!! Such types of errors demand type casting in Python and we will do the same below.
To apply type casting in Python, we need to cast the data type by passing the variable in the data type constructor similar to passing the parameter in a function. For example, we can convert an integer to a string using str(variable_name), an integer will convert to oct notation using oct (variable_name), and so on.
Keeping in sync with the same example, we shall use the typecasting in Python to see if we can make this work.
1 2 3 |
age = 60 string = "My age is" print(string + str(age)) |

We can also tweak our implicit conversion example to get the result in integer:
1 2 3 4 5 6 7 |
a = 2 print(type(a)) b = 3.0 b = int(b) print(type(b)) add = a + b print(type(add)) |

Key Takeaways:
- Type conversion is a process of changing one data type into another.
- Type conversion in Python is of two types: implicit type conversion and explicit type conversion.
- Implicit type conversion in Python refers to the automatic conversion of data type by Python.
- Explicit type conversion is when the user explicitly cast one data type into another.