Python Type Casting

Python Type Casting

Python type casting is the process in which data type of one variable is changed to another whenever necessary. In Python, we know that primitive data types are considered as objects, so converting the data type of a variable can be done using the constructors of the data type class.

 

 

Types of Python Type Casting

In Python, there are two types of Type Casting, namely:

  • Implicit Type Casting
  • Explicit Type Casting

 

Implicit Type Casting

When data type of a variable is changed to some other data type automatically, it is called implicit type casting. It happens without intervention of the user or the programmer.

Example:

# Python program to exemplify implicit type casting

# variable_int converted to int type
variable_int=3
print(variable_int," is of ",type(variable_int))

# variable_string converted to string type
variable_string="You are at StudyExperts"
print(variable_string," is of ",type(variable_string))

# variable_float converted to float type
variable_float=2.3
print(variable_float," is of ",type(variable_float))

Output:

3  is of  <class 'int'>
You are at StudyExperts  is of  <class 'str'>
2.3  is of  <class 'float'>

 

 

Explicit Type Casting

When data type of a variable is changed to some other data type manually, it is called explicit type casting. It happens with intervention of the user or the programmer. Functions used for this purpose are provided below:

      • int() – It converts different data types into int data type. It takes a float or a string literal(which should represent a whole number) and converts it into int data type.
      • float() – It converts different data types into float data type. It takes an integer or a string literal(which should represent a whole number) and converts it into float data type.
      • str() – It converts different data types into string data type. 

Type Casting int to float:

Conversion of int to float requires use of float() constructor of float class.

# Python program to exemplify explicit type casting of int to float

var=2
print("var before explicit type casting is of ",type(var))

var=float(var)
print("var after explicit type casting is of ",type(var))

Output:

var before explicit type casting is of  <class 'int'>
var after explicit type casting is of  <class 'float'>

Type Casting float to int:

Conversion of float to int requires use of int() constructor of int class.

# Python program to exemplify explicit type casting of float to int

var=2.8
print("var before explicit type casting is ",var," of ",type(var))

var=int(var)
print("var after explicit type casting is ",var," of ",type(var))

Output:

var before explicit type casting is  2.8  of  <class 'float'>
var after explicit type casting is  2  of  <class 'int'>

Type Casting int to string:

Conversion of int to string requires use of str() constructor of str class.

# Python program to exemplify explicit type casting of int to string

var=2
print("var before explicit type casting is ",var," of ",type(var))

var=str(var)
print("var after explicit type casting is ",var," of ",type(var))

Output:

var before explicit type casting is  2  of  <class 'int'>
var after explicit type casting is  2  of  <class 'str'>

 

Type Casting string to int:

Conversion of string to int requires use of int() constructor of int class.

# Python program to exemplify explicit type casting of string to int

var="2"
print("var before explicit type casting is ",var," of ",type(var))

var=int(var)
print("var after explicit type casting is ",var," of ",type(var))

Output:

var before explicit type casting is  2  of  <class 'str'>
var after explicit type casting is  2  of  <class 'int'>

 

Type Casting string to float:

Conversion of string to float requires use of float() constructor of float class.

# Python program to exemplify explicit type casting of string to float

var="2.9"
print("var before explicit type casting is ",var," of ",type(var))

var=float(var)
print("var after explicit type casting is ",var," of ",type(var))

Output:

var before explicit type casting is  2.9  of  <class 'str'>
var after explicit type casting is  2.9  of  <class 'float'>

 

Share this post

Leave a Reply

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