Related Articles

Related Articles

Type Casting in Python (Implicit and Explicit) with Examples
  • Last Updated : 02 Dec, 2020

Prerequisites: Python Data Types

Type Casting is the method to convert the variable data type into a certain data type in order to the operation required to be performed by users. In this article, we will see the various technique for typecasting.

There can be two types of Type Casting in Python –

  • Implicit Type Casting
  • Explicit Type Casting

Implicit Type Conversion

In this,  methods, Python converts data type into another data type automatically. In this process, users don’t have to involve in this process. 

Python3



filter_none

edit
close

play_arrow

link
brightness_4
code

# Python program to demonstrate 
# implicit type Casting 
  
# Python automatically converts 
# a to int 
a = 7
print(type(a)) 
  
# Python automatically converts 
# b to float 
b = 3.0
print(type(b)) 
  
# Python automatically converts 
# c to int as it is a floor addition 
c = a +
print(c) 
print(type(c))
  
# Python automatically converts 
# c to int as it is a floor multiplication
d = a * b
print(d)
print(type(d))

chevron_right


Output:

<class 'int'>
<class 'float'>
10.0
<class 'float'>
21.0
<class 'float'>

Explicit Type Casting

In this method, Python need user involvement to convert the variable data type into certain data type in order to the operation required.

Mainly in type casting can be done with these data type function:

  • Int() : Int() function take float or string as an argument and return int type object.
  • float() : float() function take int or string as an argument and return float type object.
  • str() : str() function take float or int as an argument and return string type object.

Let’s see some example of type casting:

Type Casting int to float:

Here, we are casting integer object to float object with float() function.

Python3

filter_none

edit
close

play_arrow

link
brightness_4
code

# Python program to demonstrate 
# type Casting 
  
# int variable
a = 5
  
# typecast to float
n = float(a)
  
print(n)
print(type(n))

chevron_right


Output:

5.0
<class 'float'>

Type Casting float to int:



Here, we are casting float data type into integer data type with int() function.

Python3

filter_none

edit
close

play_arrow

link
brightness_4
code

# Python program to demonstrate 
# type Casting 
  
# int variable
a = 5.9
  
# typecast to int
n = int(a)
  
print(n)
print(type(n))

chevron_right


Output:

5
<class 'int'>

Type casting int to string:

Here, we are casting int data type into string data type with str() function.

Python3

filter_none

edit
close

play_arrow

link
brightness_4
code

# Python program to demonstrate 
# type Casting 
  
# int variable
a = 5
  
# typecast to str
n = str(a)
  
print(n)
print(type(n))

chevron_right


Output:

5
<class 'str'>

Type Casting string to int:

Here, we are casting string data type into integer data type with int() function.

Python3

filter_none

edit
close

play_arrow

link
brightness_4
code

# Python program to demonstrate 
# type Casting 
  
# string variable
a = "5"
  
# typecast to int
n = int(a)
  
print(n)
print(type(n))

chevron_right


Output:

5
<class 'int'>

Type Casting String to float:

Here, we are casting string data type into float data type with float() function.

Python3

filter_none

edit
close

play_arrow

link
brightness_4
code

# Python program to demonstrate 
# type Casting 
  
# string variable
a = "5.9"
  
# typecast to float
n = float(a)
  
print(n)
print(type(n))

chevron_right


Output:

5.9
<class 'float'>

Attention geek! Strengthen your foundations with the Python Programming Foundation Course and learn the basics.

To begin with, your interview preparations Enhance your Data Structures concepts with the Python DS Course.

My Personal Notes arrow_drop_up
Recommended Articles
Page :