Python is a high-level, interpreted programming language, and is one of the most popular programming languages present in the software industry. One of the most important but often overlooked concepts in python is that of casting. Let us have a look at it in detail.
In python, casting is basically a concept used to specify or change the type of a variable. Unlike other languages, everything in python is represented as an object instead of declaring the datatype when you initialize the variable.
Casing in python is of two types - implicit and explicit. Implicit type casting is automatically performed by the python interpreter.
If you want a variable to contain data of a particular type, we use explicit casting.
Since python is also an object-oriented programming language, the data types are represented with the help of classes.
Therefore, we can use the constructors of these classes to perform explicit casting of data types.
As mentioned before, the python interpreter also performs implicit casting automatically whenever required.
In this case, we have two variables a and b. The variable is of int data type and has value 2 whereas the latter is of float data type and has value 3.0. We add these variables and store the sum in c, we can see that c automatically gets the data type of float. Note that we can use the type() function in python to see the data type class of a variable. Let us look at the python code and corresponding output for this example -
# integer variable
a = 2
print(a)
print(type(a))
# float variable
b = 3.0
print(b)
print(type(b))
# Adding these
c = a+b
print(c)
print(type(c))
Output
2
<class 'int'>
3.0
<class 'float'>
5.0
<class 'float'>
The syntax for explicit type casting is very straightforward and is as follows. We simply use the constructor of the class of the target data type and pass the variable as an argument to it.
<constructor_name> (<variable_name>)
Let us look at some examples of explicit casting.
Here, we have a variable with a float value of 2.1. If we convert it to int, we will obviously lose the value after the decimal point and it becomes just 2.
We use the constructor int() for this. Note that we can use the type() function in python to see the data type class of a variable.
Let us look at the python code and corresponding output for this example.
# Creating float variable
a = 2.1
print(a)
print(type(a))
# Casting float to int
a = int(a)
print(a)
# Checking variable type after conversion
print(type(a))
Output
2.1
<class 'float'>
2
<class 'int'>
Here, we have a variable with a string value of 2 and we need to convert it to int. We use the constructor int() for this.
Note that we can use the type() function in python to see the data type class of a variable.
Let us look at the python code and corresponding output for this example.
# Creating string variable
a = "2"
print(a)
print(type(a))
# Casting string to int
a = int(a)
print(a)
# Checking variable type after conversion
print(type(a))
Output
2
<class 'str'>
2
<class 'int'>
Similarly, we can convert int to float in python with explicit casting. Here, we have a variable with an int value of 2.
If we convert it to float, it becomes 2.0. We use the constructor float() for this.
Let us look at the python code and corresponding output for this example.
# Creating int variable
a = 2
print(a)
print(type(a))
# Casting int to float
a = float(a)
print(a)
# Checking variable type after conversion
print(type(a))
Output
2
<class 'int'>
2.0
<class 'float'>
As mentioned before, we can use explicit type casting to convert variables from one data type to another. Here, we have a variable with an int value of 2 and we need to convert it to string.
We use the constructor str() for this. Let us look at the python code and corresponding output for this example -
# Creating int variable
a = 2
print(a)
print(type(a))
# Casting int to string
a = str(a)
print(a)
# Checking variable type after conversion
print(type(a))
Output
2
<class 'int'>
2
<class 'str'>
As mentioned before, we can use explicit type casting to convert variables from one data type to another. Here, we have a variable with a float value of 2.1 and we need to convert it to string.
We use the constructor str() for this. Note that we can use the type() function in python to see the data type class of a variable.
Let us look at the python code and corresponding output for this example -
# Creating float variable
a = 2.1
print(a)
print(type(a))
# Casting float to string
a = str(a)
print(a)
# Checking variable type after conversion
print(type(a))
Output
2.1
<class 'float'>
2.1
<class 'str'>
Here, we have a variable with a string value of 2.1 and we need to convert it to float. We use the constructor float() for this.
Let us look at the python code and corresponding output for this example.
# Creating string variable
a = "2.1"
print(a)
print(type(a))
# Casting string to float
a = float(a)
print(a)
# Checking variable type after conversion
print(type(a))
Output
2.1
<class 'str'>
2.1
<class 'float'>
In this topic, we have learned the use and advantages of casting in a Python program, following some simple running examples of programs, thus giving us an intuition of how this concept could be applied in real-world situations. Feel free to reach out to info.javaexercise@gmail.com in case of any suggestions.