In this python type casting/ conversion tutorial, you will learn python type casting/conversion with example.
This tutorial has purpose to explain to you what is type casting in python and how to use this with data?.
What is type conversion?
Generally, convert one data type variable to another/different data type is called type conversion in python.
For example: If you have a variable whose type is an integer. And you want to change it to float data type. Then you have to use float() method for that.
In this way, you can change any data type variable to many more data types such as integer, float, string, etc.
Types of Type Conversion in python
There are two types of conversions in Python. They are:
- Implicit type conversion
- Explicit type conversion
Implicit type conversion
The Implicit type conversion in Python, Python automatically converts one data type to a different data type. This process does not require any user involvement.
Example 1: Converting integer to float
no_int = 111 no_flo = 11.23 no_new = no_int + no_flo print("datatype of no_int:",type(no_int)) print("datatype of no_flo:",type(no_flo)) print("Value of no_new:",no_new) print("datatype of no_new:",type(no_new))
Above program explanation,
- We have added two variables no_int and no_flo and then storing the value in no_new variable.
- We will look at the data type of all three objects respectively.
- In the output we can see the datatype of no_int is an
integer
, datatype of no_flo is a float. - Also, we can see the no_new has
float
data type because Python always converts smaller data type to larger data type to avoid the loss of data.
The output of the above program is following:
datatype of num_int: <class 'int'> datatype of num_flo: <class 'float'> Value of num_new: 122.23 datatype of num_new: <class 'float'>
Explicit type conversion
Explicit type conversion in Python converts one data type into another data type as per user requirement. For this, the use of Python’s predefined function such as fixed (), int() is used.
The syntax for explicit type conversion is:
(required_datatype)(expression)
This type of conversion is also called typecasting because the user modifies (changes) the data type of the objects.
Example 1:
no_int = 11 no_str = "555" print("Data type of no_int:",type(no_int)) print("Data type of no_str before Type Casting:",type(no_str)) no_str = int(no_str) print("Data type of no_str after Type Casting:",type(no_str)) no_sum = no_int + no_str print("Sum of no_int and no_str:",no_sum) print("Data type of the sum:",type(no_sum))
Above program explanation,
- In the above program, we have added no_str and no_int variable.
- We have converted no_str from string(higher) to integer(lower) type using int() function to perform the addition.
- After converting no_str to a integer value Python is able to add these two variable.
- We got the no_sum value and data type to be integer.
The output of the above program is the following:
Data type of no_int: <class 'int'> Data type of no_str before Type Casting: <class 'str'> Data type of no_str after Type Casting: <class 'int'> Sum of no_int and no_str: 566 Data type of the sum: <class 'int'>