Type Conversion In Python

Type Conversion in Python

 
The process of converting the value of one data type into another type is called Type Conversion. Python has two types of type conversion.
  • Implicit Type Conversion
  • Explicit Type Conversion

Implicit Type Conversion

 
Python automatically converts one data type into another data type. This process doesn't need any user involvement. A few examples of this are given below.
 
Example 1
  1. num_int = 123   
  2. num_flo = 1.23  
  3. num_new = num_int + num_flo  
  4. print('type of num_int is : ', type(num_int))  
  5. print('type of num_flo is : ', type(num_flo))  
  6. print('value of num_new is : ', num_new)  
  7. print('type of num_new is : ', type(num_new))  
Type Conversion In Python
 
In the above example, based on the value type of the variable, num_int is an integer data type, num_flo is a float data type, and finally, num_new is a float data type. Here, implicit type conversion takes place because the addition of integer and float results in a float value (smaller data type automatically converts to the larger data type).
 
Example 2
  1. try:  
  2.     num_int = 123   
  3.     num_str = '456'  
  4.     num_new = num_int + num_str  
  5.     print('type of num_int is : ', type(num_int))  
  6.     print('type of num_str is : ', type(num_str))  
  7.     print('value of num_new is : ', num_new)  
  8.     print('type of num_new is : ', type(num_new))  
  9. except TypeError as typeError:  
  10.     print(typeError)  
Type Conversion In Python
 
In the above example, based on the value type of the variable, num_int is an integer data type, num_str is a string data type. Here, implicit type conversion cannot take place because integer and string values cannot be added which results in the type error.
 

Explicit Type Conversion

 
Python defines type conversion functions like int(), float(), str() to directly convert one data type into another. This type of conversion is also called typecasting because the user casts (change) the data type of the objects.
 
int(a, base)
 
This function converts any data type into integer. ‘Base’ specifies the base in which a string is converted if the data type is string. By default, ‘Base’ is set to 10.
  1. try:  
  2. s = "10010"    
  3. # string converting to int base 2   
  4. c = int(s,2)   
  5. print('string converted to int base 2 : ', c)  
  6. # string converting to int base 10   
  7. c = int(s)   
  8. print('string converted to int base 10: ', c)  
  9. except TypeError as typeError:  
  10. print(typeError)  
Type Conversion In Python
 
float()
 
This function is used to convert any data type to a floating point number.
  1. try:  
  2.     s = "10010"    
  3.     # string converting to float   
  4.     c = float(s)   
  5.     print('string converted to float: ', c)  
  6. except TypeError as typeError:  
  7.     print(typeError)  
Type Conversion In Python
 
ord()
 
This function is used to convert a character into integer (ascii value of character).
  1. try:  
  2.     s = '4'    
  3.      #ord method returns ascii value of character passed in  
  4.     c = ord(s)  
  5.     print("ascii value of '4' : ", c)  
  6. except TypeError as typeError:  
  7.     print(typeError)  
Type Conversion In Python
 
hex()
 
This function is used to convert an integer into a hexadecimal string. 
  1. try:  
  2.     s = 4  
  3.     c = hex(s) #converts 4 into hexadecimal value  
  4.     print("hexadecimal value of 4 : ", c)  
  5. except TypeError as typeError:  
  6.  print(typeError)  
Type Conversion In Python
 
Passing the types other than integer to hex() method raises an error. 
  1. try:  
  2.     s = '4'  
  3.     c = hex(s) #converts 4 into hexadecimal value  
  4.     print("hexadecimal value of 4 : ", c)  
  5. except TypeError as typeError:  
  6.     print(typeError)  
Type Conversion In Python
 
oct()
 
This function converts an integer into an octal string. 
  1. try:        
  2.     s = 4    
  3.     c = oct(s)     
  4.     print("value converted into octal value:  ", c)    
  5. except TypeError as typeError:    
  6.     print(typeError)    
Type Conversion In Python
 
tuple()
 
This function is used to convert a data type into a tuple.
  1. try:  
  2.     s = 'python'    
  3.     c = tuple(s)  
  4.     print("string converted into tuple:  ", c)  
  5. except TypeError as typeError:  
  6. print(typeError)  
Type Conversion In Python
 
set()
 
This function returns the type after converting into set.
  1. try:  
  2.     s = 'python programmers'    
  3.     c = set(s)   
  4.     print("string converted into set:  ", c)  
  5. except TypeError as typeError:  
  6.     print(typeError)  
Type Conversion In Python
 
list()
 
This function is used to convert any data type into a list type.
  1. try:  
  2.     s = 'python'    
  3.     c = list(s)   
  4.     print("string converted into list:  ", c)  
  5. except TypeError as typeError:  
  6.     print(typeError)  
Type Conversion In Python
 
dict()
 
This function is used to convert a tuple of order (key, value) into a dictionary.
  1. try:  
  2.     tup = (('a'1), ('f'2), ('g'3))  
  3.     c = dict(tup)   
  4.     print("tuple converted into dictionary:  ", c)  
  5. except TypeError as typeError:  
  6.     print(typeError)  
Type Conversion In Python
 
str()
 
This function is used to convert a value (integer or float) into a string.
  1. try:      
  2.     f = 10.0  
  3.     s = str(f)   
  4.     print("float value converted into string:  ", s)  
  5. except TypeError as typeError:  
  6.     print(typeError)  
Type Conversion In Python
 

Summary

 
In this article, we looked at the different ways we can convert one data type into another, in Python.


Similar Articles