Explicit Type Casting in Python Language

All of us can declare and work with data types. Have we ever wondered about their interconversion? In this article, we learn about how we can convert these data types using inbuilt functions in Python a.k.a Type Casting. Type Casting is of two types: Implicit & Explicit. In this module, we are going to discuss about Explicit type casting only.
Now let’s have a look at some basic & type conversions
The int() function allows us to convert any data type to integer. It accepts exactly two parameters namely Base and Number where Base signifies the base of which integer value belongs to ( binary[2], octal[8], hexadecimal[16]).
The float() function allows us to convert any data type to a floating point number. It accepts exactly one parameter i.e. the value of data type that needs to be converted.
#Type casting
   value = "0010110"
   # int base 2
p = int(value,2)
print ("integer of base 2 format : ",p)
# default base
d=int(value)
print ("integer of default base format : ",d)
   # float
e = float(value)
print ("corresponding float : ",e)
integer of base 2 format : 22
integer of default base format : 10110
corresponding float : 10110.0
In the above code, we have also converted integer with base in Python
The tuple() function allows us to convert to a tuple. It accepts exactly one parameter either a string or a list.
The list() function allows us to convert any data type to a list type. It accepts exactly one parameter.
The dict() function is used to convert a tuple of order (key, value) into a dictionary. Key must be unique in nature otherwise duplicate value gets overridden.
#Type casting
   str_inp = 'Tutorialspoint'
# converion to list
j = list(str_inp)
print ("string to list : ")
print (j)
# conversion to tuple
i = tuple(str_inp)
print ("string to tuple : ")
print (i)
# nested tuple
tup_inp = (('Tutorials', 0) ,('Point', 1))
# conversion to dictionary
c = dict(tup_inp)
print ("list to dictionary : ",c)
# nested list
list_inp = [['Tutorials', 0] ,['Point', 1]]
# conversion to dictionary
d = dict(list_inp)
print ("list to dictionary : ",d)
string to list :
['T', 'u', 't', 'o', 'r', 'i', 'a', 'l', 's', 'p', 'o', 'i', 'n', 't']
string to tuple :
('T', 'u', 't', 'o', 'r', 'i', 'a', 'l', 's', 'p', 'o', 'i', 'n', 't')
list to dictionary : {'Tutorials': 0, 'Point': 1}
list to dictionary : {'Tutorials': 0, 'Point': 1}
The str() function is used to convert integer or float into a string. It accepts exactly one argument.
chr()-This function is used to convert a integer type to character type.
ord()-This function is used to convert a character type to integer type.
#Type casting
   char_inp = 'T'
#converting character to corresponding integer value
print ("corresponding ASCII VALUE: ",ord(char_inp))
   int_inp=92
#converting integer to corresponding Ascii Equivalent
print ("corresponding ASCII EQUIVALENT: ",chr(int_inp))
#integer and float value
inp_i=231
inp_f=78.9
# string equivalent
print ("String equivalent",str(inp_i))
print ("String equivalent",str(inp_f))
corresponding ASCII VALUE: 84
corresponding ASCII EQUIVALENT: \
String equivalent 231
String equivalent 78.9
In this article, we learnt about explicit type casting in Python 3.x. Or earlier.