Overview
It is normal to get stuck in programming, especially when you get into deep! During the Python typecasting you must have faced data type-changing problems like x = ‘1’, y = ‘2’, z = x+y = 12 instead of 3. But how and why? You must be frustrated too while thinking of this!
What’s Next!
In this article, learn the Implicit and Explicit of Python type conversion with our comprehensive guide. Discover how to convert data types effortlessly, from integers to strings and beyond. Enhance your Python programming skills and tackle type conversion challenges with confidence.
Table of Contents
Before jumping out to the types of Python Typecasting, let’s take a look at the common typecasting problem.
So, we have an example:
x = '1'
y = '2'
z = x+y
print(z)
So, here we got z = 12, instead of 3. Why? To the point, it is because both numbers are stored in single-quote, which is used to define an object type as a string therefore it leads to making a number data type string instead of an int.
Type Conversion
Type Conversion is the process of converting one data type to another one.
Python holds two types of Conversion –
Implicit
Explicit
Let’s get started with Implicit Type Conversion.
Implicit
Implicit Type Conversion is an automated process meaning, Python converts one data type to another one itself, we don’t need to manually define which data type has to convert.
Example: Conversion of int to float.
x_int = 4
y_float = 5.0
sum = x_int+y_float
print("Datatype of x_int:", type(x_int))
print("Datatype of y_float:", type(y_float))
print("Sum:", sum)
print("Datatype of Sum:", type(sum))
#output:
Datatype of x_int:
Datatype of y_float:
Sum: 9.0
Datatype of Sum:
Example: Add string with an integer.
x_str = "110"
y_int = 5
conversion = x_str + y_int
print("Datatype of x_str:", type(x_str))
print("Datatype of y_int:", type(y_int))
print("New number:", conversion)
#output:
TypeError: can only concatenate str (not "int") to str.
Since a string data type variable cannot be added to int data type variable, therefore Python returned an error. Here, Implicit Type Conversion is not right option to choose instead Explicit Type Conversion is the best option to handle these kind of problems.
Explicit
Explicit Type Conversion is the opposite of Implicit meaning, it is a manual process of converting one data type to another one. We have to specify in Python which data type has to be converted to which data type.
For the manual conversion, Python provides its data type such as int(), float(), str(), etc.
In programming language, this type of conversion known as typecasting because Python casts (changes) the data type of the objects.
Syntax:
(expression)
Example: Add string with an integer.
x_int = 5
y_str = "10"
print("Data type of x_int:", type(x_int))
print("Data type of y_str before Type Casting:", type(y_str))
print("Sum of both x_int and y_str:", x_int + y_str)
#output:
TypeError: unsupported operand type(s) for +: 'int' and 'str'
Now, let’s manually defined y_str has to convert to which data type by using Explicit Type Conversion.
x_int = 5
y_str = "10"
new_y_str = int(y_str)
print("Data type of y_str after Type Casting:", type(new_y_str))
z_sum = x_int + new_y_str
print("z_sum:", z_sum)
Note: Comment the line number 8.
#output:
Data type of x_int:
Data type of y_str before Type Casting:
Data type of y_str after Type Casting:
z_sum: 15
Example: Conversion of float to int.
x_int = 4
y_float = 5.0
new_y_float = int(y_float)
sum = x_int + new_y_float
print("Sum:", sum)
#output:
Sum: 9
Example: Conversion of str to float.
x_int = '4'
y_float = 5.0
new_x_int = int(x_int)
sum = new_x_int + y_float
print("Sum:", sum)
print(type(sum))
#output:
Sum: 9.0
Example: Conversion of float to int.
x_int = 4.0
y_float = 5.0
new_x_int = int(x_int)
new_y_float = int(y_float)
sum = new_x_int + new_y_float
print("Sum:", sum)
print(type(sum))
#output:
Sum: 9
Example: Conversion of Complex to int.
x_int = 4
y_float = 1+2j
new_y_float = int(y_float)
sum = x_int + new_y_float
print("Sum:", sum)
#output:
TypeError: int() argument must be a string, a bytes-like object or a real number, not 'complex'.
Python does not allow to change the type of complex data type into another data type.
Conclusion
By mastering type conversion in Python, you’ll gain the ability to seamlessly handle different data types, optimize your code, and overcome common programming challenges. So, let’s dive into the world of Python-type conversion and unlock the full potential of your programming skills.
Throughout this guide, we have provided practical examples to illustrate the concepts and help you apply type conversion techniques effectively in your Python projects.