Type Conversion & Type Casting in Python

Type Conversion

Type conversion refers to the act of converting the data type of a value to another datatype. Usually, the need of type conversion arises when there are more than one type of values used in an expression. The result of such operations is converted into the higher data type. For example, the sum of an integer value and float value will be a float value.

Type conversion is also known as Implicit Type Conversion because it is an automatic process performed by the python interpreter. The interpreter itself identifies the need of type conversion and changes the value from lower datatype to higher datatype. This prevents the loss of data.

Conversion of an integer to float.

Example:

a = 10 #integer value
print('a =', a)
print('Type of a =', type(a))

b = 5.78 #float value
print('b =', b)
print('Type of b =', type(b))

c = a + b
print('c =', c)
print('Type of c =', type(c))?

Output:

a = 10
Type of a = <class 'int'>

b = 5.78
Type of b = <class 'float'>

c = 15.780000000000001
Type of c = <class 'float'>?

In the above program, we have created variables a & b and assigned an integer & a float value to them respectively. On adding the values of a & b, the result generated is a float value. Here, the interpreter has converted the result into the higher datatype out of integer and float i.e. float.

Conversion of an integer to string.

Example:

p = 'hello'
print('p =', p)
print('Type of p =', type(p))

q = 101
print('q =', q)
print('Type of q =', type(q))

sum = p+q
print('sum =', sum)
print('Type of sum =', type(sum))?

Output: error

p = hello
Type of p = <class 'str'>

q = 101
Type of q = <class 'int'>

Traceback (most recent call last):
File "C:/Users/TS7/AppData/Local/Programs/Python/Python37/type conversion.py", line 9, in <module>
sum = p+q
TypeError: can only concatenate str (not "int") to str?

In the above program, as the interpreter reaches the line sum=p+q, an error arises. The reason is that the value of integer (number) type cannot be converted to string type implicitly(by the interpreter).

Then how can we overcome this problem? The solution is Type casing.

Type Casting

Type casting is another way to perform type conversion. It is an explicit process which means it is done by the programmer manually. It is also called as Explicit Type Conversion. Explicit conversion can convert an integer to a string whereas implicit type conversion can not.

  • Python provides some built-in functions to cast(convert) a value into another type like int(), float() etc.
  • The variable or the value which is to be converted is passed as the argument to these functions.

Syntax:
new_type(variable/value)

Let us perform typecasting in the previous example to convert an integer to a string.

p = 'hello'
print('p =', p)
print('Type of p =', type(p))

q = 101
print('q =', q)
print('Type of q =', type(q))

q = str(q) #type casting integer to string.
print('After conversion------ q =', q)
print('Type of q =', type(q))

sum = p+q
print('sum =', sum)
print('Type of sum =', type(sum))

Output:

p = hello
Type of p = <class 'str'>

q = 101
Type of q = <class 'int'>

After conversion------ q = 101
Type of q = <class 'str'>

sum = hello101
Type of sum = <class 'str'>
  • You can also convert a sequence(list, tuples etc.) into another datatype with functions like tuple(), list() etc.

Example:

data = [23,67,45,88,12]
print('data =', data)
print('Type of data =', type(data))

data = tuple(data)
print('After type casting----data =', data)
print('Type of data =', type(data))

Output:

data = [23, 67, 45, 88, 12]
Type of data = <class 'list'>

After type casting----data = (23, 67, 45, 88, 12)
Type of data = <class 'tuple'>

In this program, a list named data is explicitly converted into a tuple. You can identify the conversion in the output with the help of a change in the brackets surrounding the sequence.

In this way, you can convert the value into different datatypes. Various built-in functions to perform type casting in python are listed below. You can create programs using them to see the results.

Function Use
int() to convert a value to integer type.
float()  to convert a value to float type.
complex()  to convert a value to complex type.
long()  to convert a value to long integer type.
str() to convert a value to string type.
chr()  to convert an integer into a character.
list()  to convert a sequence into a list.
tuple() to convert a sequence into a tuple.
set() to convert a sequence into a set.