Type Casting is the method to convert the Python variable datatype into a certain data type in order to perform the required operation by users. We will see various techniques for typecasting. There can be two types of Type Casting in Python:
- Implicit Type Conversion
- Explicit Type Conversion
Example: The following code converts a numeric string into an integer so it can be used in arithmetic operations.
age = "21"
age = int(age)
print(age + 5)
print(type(age))
Output
26 <class 'int'>
Implicit Type Conversion
Implicit type conversion is the automatic changing of a data value from one type to another by the compiler or runtime environment without any manual intervention from the programmer
# Python automatically converts 'a' to int
a = 7
print(type(a))
# Python automatically converts 'b' to float
b = 3.0
print(type(b))
# Python automatically converts 'c' to float as it is a float addition
c = a + b
print(c)
print(type(c))
# Python automatically converts 'd' to float as it is a float multiplication
d = a * b
print(d)
print(type(d))
Output
<class 'int'> <class 'float'> 10.0 <class 'float'> 21.0 <class 'float'>
Explanation:
- a is an integer and b is a floating-point number.
- During a + b, Python automatically converts a to float, so c becomes 10.0.
- During a * b, Python again converts a to float, so d becomes 21.0.
- Since the operations involve both int and float, the results are returned as float.
- This automatic conversion is known as implicit type conversion.
Explicit Type Conversion
Explicit type conversion is when the programmer manually changes a value’s data type using built-in type casting functions, usually when automatic conversion is not possible or a specific type is needed.
Examples of Type Casting
Commonly used type casting functions in Python are:
- Int() function take float or string as an argument and returns int type object.
- float() function take int or string as an argument and return float type object.
- str() function takes float or int as an argument and returns string type object.
Convert Int to Float
Converting Int to Float in with the float() function.
a = 5
n = float(a)
print(n)
print(type(n))
Output
5.0 <class 'float'>
Python Convert Float to Int
Converting Float to int datatype in Python with int() function.
a = 5.9
n = int(a)
print(n)
print(type(n))
Output
5 <class 'int'>
Python Convert int to String
Converting int to String datatype in Python with str() function.
a = 5
# typecast to str
n = str(a)
print(n)
print(type(n))
Output
5 <class 'str'>
Python Convert String to float
Casting string data type into float data type with float() function.
a = "5.9"
n = float(a)
print(n)
print(type(n))
Output
5.9 <class 'float'>
Python Convert string to int
Converting string to int datatype in Python with int() function. If the given string is not number, then it will throw an error.
a = "5"
b = 't'
n = int(a)
print(n)
print(type(n))
print(int(b))
print(type(b))
Output
5
<class 'int'>
---------------------------------------------------------------------------
ValueError Traceback (most recent call last)
/tmp/ipython-input-1957009883.py in <cell line: 0>()
9 print(type(n))
10
---> 11 print(int(b))
12 print(type(b))
ValueError: invalid literal for int() with base 10: 't'
The string "5" is successfully converted into an integer 5. Since 't' is not a number, Python cannot convert it into an integer.
Adding String and Integer Without Conversion
If we try to directly add an integer and a string, Python will throw an error because they are different data types.
a = 5
b = 't'
n = a+b
print(n)
print(type(n))
Output
TypeError: unsupported operand type(s) for +: 'int' and 'str'
We should first convert the string into an integer and then add:
a = 5
b = '7'
# explicit conversion of string to int
n = a + int(b)
print(n)
print(type(n))
Output
12 <class 'int'>