Type Casting in Python (Implicit and Explicit) with Examples
Last Updated :
07 Aug, 2024
Type Casting is the method to convert the Python variable datatype into a certain data type in order to perform the required operation by users. In this article, we will see the various techniques for typecasting. There can be two types of Type Casting in Python:
- Python Implicit Type Conversion
- Python Explicit Type Conversion
Implicit Type Conversion in Python
In this, method, Python converts the datatype into another datatype automatically. Users don't have to involve in this process.
Python
# 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 float as it is a float addition
c = a + b
print(c)
print(type(c))
# Python automatically converts
# d to float as it is a float multiplication
d = a * b
print(d)
print(type(d))
Output:
<class 'int'>
<class 'float'>
10.0
<class 'float'>
21.0
<class 'float'>
Explicit Type Conversion in Python
In this method, Python needs user involvement to convert the variable data type into the required data type.
Examples of Type Casting in Python
Mainly type casting can be done with these data type functions:
- Int(): Python Int() function take float or string as an argument and returns int type object.
- float(): Python float() function take int or string as an argument and return float type object.
- str(): Python str() function takes float or int as an argument and returns string type object.
Python Convert Int to Float
Here, we are Converting Int to Float in Python with the float() function.
Python
# Python program to demonstrate
# type Casting
# int variable
a = 5
# typecast to float
n = float(a)
print(n)
print(type(n))
Output:
5.0
<class 'float'>
Python Convert Float to Int
Here, we are Converting Float to int datatype in Python with int() function.
Python
# Python program to demonstrate
# type Casting
# int variable
a = 5.9
# typecast to int
n = int(a)
print(n)
print(type(n))
Output:
5
<class 'int'>
Python Convert int to String
Here, we are Converting int to String datatype in Python with str() function.
Python
# Python program to demonstrate
# type Casting
# int variable
a = 5
# typecast to str
n = str(a)
print(n)
print(type(n))
Output:
5
<class 'str'>
Python Convert String to float
Here, we are casting string data type into float data type with float() function.
Python
# Python program to demonstrate
# type Casting
# string variable
a = "5.9"
# typecast to float
n = float(a)
print(n)
print(type(n))
Output:
5.9
<class 'float'>
Python Convert string to int
Here, we are Converting string to int datatype in Python with int() function. If the given string is not number, then it will throw an error.
Python
# string variable
a = "5"
b = 't'
# typecast to int
n = int(a)
print(n)
print(type(n))
print(int(b))
print(type(b))
Output:
5
<class 'int'>
---------------------------------------------------------------------------
ValueError Traceback (most recent call last)
Cell In[3], line 14
11 print(n)
12 print(type(n))
---> 14 print(int(b))
15 print(type(b))
ValueError: invalid literal for int() with base 10: 't'
Addition of string and integer Using Explicit Conversion
Python
# integer variable
a = 5
# string variable
b = 't'
# typecast to int
n = a+b
print(n)
print(type(n))
Output:
TypeError Traceback (most recent call last)
Cell In[5], line 10
7 b = 't'
9 # typecast to int
---> 10 n = a+b
12 print(n)
13 print(type(n))
Similar Reads
Primitive Data Types vs Non Primitive Data Types in Python Python, known for its versatility and user-friendliness, is a dynamic language that doesn't explicitly categorize data types into primitive and non-primitive as some languages like Java do. However, for clarity and understanding, it's useful to draw parallels and discuss similar concepts. In this ar
4 min read
Comparing Old-Style and New-Style Classes in Python In Python, the difference between old-style and new-style classes is based on the inheritance from the built-in object class. This distinction was introduced in Python 2.x and was fully adopted in Python 3.x, where all classes are new-style classes. In this article, we will see the difference betwee
4 min read
Python Numbers, Type Conversion and Mathematics Prerequisite: Python Language Introduction Python is a general-purpose interpreted, interactive, object-oriented, and high-level programming language. It was created by Guido van Rossum. It is an open-source programming language. Types of numbers in Python There are three numeric types in Python: in
4 min read
Last Minute Notes (LMNs) â Data Structures with Python Data Structures and Algorithms (DSA) are fundamental for effective problem-solving and software development. Python, with its simplicity and flexibility, provides a wide range of libraries and packages that make it easier to implement various DSA concepts. This "Last Minute Notes" article offers a q
15+ min read
type and isinstance in Python In this article, we will cover about type() and isinstance() function in Python, and what are the differences between type() and isinstance(). What is type in Python? Python has a built-in method called type which generally comes in handy while figuring out the type of the variable used in the progr
5 min read