Computer >> Computer tutorials >  >> Programming >> Python

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

Integer Type Conversion in Python

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]).

Float Type Conversion in Python

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.

Example

#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)

Output

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

Tuple Type Conversion in Python

The tuple() function allows us to convert to a tuple. It accepts exactly one parameter either a string or a list.

List Type Conversion in Python

The list() function allows us to convert any data type to a list type. It accepts exactly one parameter.

Dictionary Type Conversion in Python

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.

Example

#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)

Output

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}

String Type Conversion in Python

The str() function is used to convert integer or float into a string. It accepts exactly one argument.

Ascii chr() & ord() Type Conversion in Python

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.

Example

#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))

Output

corresponding ASCII VALUE: 84
corresponding ASCII EQUIVALENT: \
String equivalent 231
String equivalent 78.9

Conclusion

In this article, we learnt about explicit type casting in Python 3.x. Or earlier.