Type Casting in Python (Implicit and Explicit) with Examples - GeeksforGeeks

30 Jun, 2021
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
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
# Python program to demonstrate# implicit type Casting # Python automatically converts# a to inta = 7print(type(a)) # Python automatically converts# b to floatb = 3.0print(type(b)) # Python automatically converts# c to float as it is a float additionc = a + bprint(c)print(type(c)) # Python automatically converts# d to float as it is a float multiplicationd = a * bprint(d)print(type(d))
Output:
<class 'int'>
<class 'float'>
10.0
<class 'float'>
21.0
<class 'float'>
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.
Type Casting int to float:
Here, we are casting integer object to float object with float() function.
Python3
# Python program to demonstrate# type Casting # int variablea = 5 # typecast to floatn = float(a) print(n)print(type(n))
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
# Python program to demonstrate# type Casting # int variablea = 5.9 # typecast to intn = int(a) print(n)print(type(n))
Output:
5
<class 'int'>
Type casting int to string:
Here, we are casting int data type into string data type with str() function.
Python3
# Python program to demonstrate# type Casting # int variablea = 5 # typecast to strn = str(a) print(n)print(type(n))
Output:
5
<class 'str'>
Type Casting string to int:
Here, we are casting string data type into integer data type with int() function.
Python3
# Python program to demonstrate# type Casting # string variablea = "5" # typecast to intn = int(a) print(n)print(type(n))
Output:
5
<class 'int'>
Type Casting String to float:
Here, we are casting string data type into float data type with float() function.
Python3
# Python program to demonstrate# type Casting # string variablea = "5.9" # typecast to floatn = float(a) print(n)print(type(n))
Output:
5.9
<class 'float'>
saurabh_maheshwari
Python-Data Type
Python
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
How to Install PIP on Windows ?
Check if element exists in list in Python
How To Convert Python Dictionary To JSON?
Python Classes and Objects
How to drop one or multiple columns in Pandas Dataframe
Python | Get unique values from a list
Defaultdict in Python
Python | os.path.join() method
Create a directory in Python
Python | Pandas dataframe.groupby()