Type Conversion and Type Casting in Python With Examples

In Python, we do not specify the variable type. But in some conditions, we have to specify it. This technique is done by the casting. By the use of the constructor function, we can access the casting in Python.

Constructor Functions

These constructor functions are given below

  • int(): It is used to construct an integer number from a float and string value. The float value will represent by rounding the first whole number. The string value will represent a whole number. Examples are given below
a = int(5)
b = int(9.2)
c = int("14")

print(a) # 5
print(b) # 9
print(c) # 14
  • float(): It is used to constructs a float number from an integer value and a string value.  These two will represent by delivering a float or an integer number. Examples are given below
a = float(15)
b = float("12")
c = float("24.12")

print(a) # 15.0
print(b) # 12.0
print(c) # 24.12
  • str(): It is used to construct a string from an integer or a float number. Examples are given below
a = str("abc1")
b = str(6)
c = str(12.05)

print(a) # 'abc1'
print(b) # '6'
print(c) # '12.05'

Python Type Conversation

Python casting is also known as python type conversation. It is very important in our daily programming challenges. Changing from one data type to another data type is known as a 'type conversation'. Implicit type conversion and explicit type conversion are two types of type of conversation in Python.

Implicit Type Conversion

Changing one data type to one data type to another data type is called implicit type conversation. Implicit type conversation does not require any user connection. We know that integer is a lower data type and float is a higher data type. The main objective is to avoid data loss when the conversion happens between lower and higher data type. An example is given below

int_number = 100
float_number = 20.45
total_number = int_number + float_number

print(type(int_number)) #<integer>
print(type(float_number)) #<float>
print(total_number) #120.45
print(type(total_number)) #<float>

Explicit Type Conversion

Users change the data type as per the programming requirement is known as explicit type conversation. We can use the constructor function (described above) to use such operations. It is also called typecasting in the programming language. An example is given below

int_number = 100
string_number = "500"

print(type(int_number)) #<integer>
print(type(string_number))#<string>

string_number = int(string_number) #changing from string to integer

print(type(string_number)) #<integer>

total = int_number + string_number

print(total) #600
print(type(total)) #<integer>

Advance Type Casting 

  • ord() function is used to convert a character to an integer.
  • hex() function is used for changing integer data type to hexadecimal data type.
  • oct() function is used to convert an integer to an octal string.
  • tuple() function is used for converting a data type to a tuple.
  • set() function is used for changing the data type to set.
  • list() function is used to convert any data type to a list.
  • dict() function is used to change to a dictionary from a tuple.
  • complex() function is used to change the real data into complex data.
  • chr() function is used to print the number depending on the ASCII value

Examples are given below,

st = '8'
print ( ord(st))

ch = hex(112)
print (ch)

ch = oct(112)
print (ch)

st = 'python'
print (tuple(st))
print (set(st))
print (list(st))

t = (('P', 10) ,('T', 20), ('N', 30))
print (complex(5,9))
print (dict(t))

Important Points

  • Type-casting is similar to the conversation of an object.
  • Python compiler itself does the Implicit Type Conversion.
  • The main focus of implicit Type Conversion is avoiding data loss.
  • With the help of existing functions, explicit type conversion is transformed.
  • If we implement the variable to a particular data type, data loss may happen.