Explicit Type Casting in Python Language

We all are familiar with the data types. Have you ever think of changing one data type to another data type. So, In this tutorial, we learn about how we can make this conversion happen using inbuilt functions in Python a.k.a Explicit Type Casting.

Type Conversion in Python with example

Now let’s look at some of the basic and soon we will take the advanced ones

Integer Type Conversion in Python

This function lets us convert any data type to integer. It has two parameters namely Base and Number where Base signifies the base of which string is if data type is string.

Float Type Conversion in Python

This function lets us convert any data type to a floating point number. It has only one parameter that is the value of data type that needs to convert.

Python Code to convert int and float

#Type casting
# using int(), float()  Builtin_methods
value = "11110"
# string getting converted to int base 2 
p = int(value,2) 
print "integer of base 2, after the conversion : ",
print p

# string getting converted when no base is mentioned 
print "After converting to integer : ",
print d
# string getting converted to float 
e = float(value) 
print "After converting to float : ",
print e
After converting to integer base 2 :  30
After converting to integer :  11110
After converting to float :  11110.0

In the above code, we have also converted integer with base in Python


Tuple Type Conversion in Python

This function lets us convert to a tuple. It also needs only one parameter.

List Type Conversion in Python

This function lets us convert any data type to a list type. It also needs only one parameter.

Dictionary Type Conversion in Python

 This function is used to convert a tuple of order (key,value) into a dictionary. Key must be unique otherwise value gets overridden.

Python Code to convert string to tuple, tuple to dictionary and string to list

#  Type casting
# using  tuple(), dict(), list() 
# initializing string 
s = 'codespeedy'
# string getting converted to tuple 
i = tuple(s) 
print "After converting string to tuple : ",
print i

# initializing new nested tuple 
tup = (('code', 1) ,('&', 1), ('speedy', 1)) 

# nested tuple getting converted to dictionary 
c = dict(tup) 
print "After converting tuple to dictionary : ",
print c 
# printing string converting to list 
j = list(s) 
print "After converting string to list : ",
print j
After converting string to tuple :  ('c', 'o', 'd', 'e', 's', 'p', 'e', 'e', 'd', 'y')
After converting tuple to dictionary :  {'code': 1, 'speedy': 1, '&': 1}
After converting string to list :  ['c', 'o', 'd', 'e', 's', 'p', 'e', 'e', 'd', 'y']

String Type Conversion in Python

Used to convert integer or float into a string. It also needs only one argument.

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

chr()-This function is used to convert a integer to character.

ord()-This function is used to convert a character to integer.

#  Type conversion 
# using  ord(),chr()

#declaring a string or character
#converting character to corresponding integer value
print ord(a)
#declaring a integer 
#converting integer to corresponding Ascii Equivalent
print chr(g)

#declaring integer and float value
#converting to corresponding string equivalent
print str(b)
print str(c)

Also, learn,
How to print string and int in the same line in Python
Tuples in Python with examples

Leave a Reply

Your email address will not be published. Required fields are marked *