Python defines type conversion functions to immediately convert one data type to another which is useful in day to day and aggressive programming. This guide is aimed at providing information about conversion functions.
Why and when to use Type-casting in Python?
Casting is when you convert a variable value from one type to another. That is, in Python, done with functions like int() or float() or str(). A pattern is that you convert a number as a string into a number.
The practice of converting the value of a single data type (integer, string, float, etc.) to a different data type is known as type conversion. Python includes two types of type conversion.
In Explicit Type Conversion, users convert the data type of the item to needed data type. We use the predefined roles such as int(), float(), str(), etc to execute explicit type conversion.
num_int = 327
num_string = "573"
print("Data type of num_int:",type(num_int))
print("Data type of num_string before Type Casting:",type(num_string))
num_string = int(num_string)
print("Data type of num_string after Type Casting:",type(num_string))
num_sum = num_int + num_string
print("Sum of num_int and num_string:",num_sum)
print("Data type of the sum:",type(num_sum))
#OutPut will be:
Data type of num_int: <class 'int'>
Data type of num_string before Type Casting: <class 'str'>
Data type of num_string after Type Casting: <class 'int'>
Sum of num_int and num_string: 900
Data type of the sum: <class 'int'>
In the preceding program:
We include num_string and num_int factor.
We switched num_string from the string(greater ) into an integer(reduced ) type utilizing int() work (get your Python homework done) to carry out the addition.
After converting num_string into some integer value Python can bring these two factors.
This procedure does not require any user participation. Let us see an instance where Python boosts the conversion of a lower data type (integer) to the greater data type (floats) to prevent data loss.
Example: Converting Int to Float
num_int = 321
num_float = 1.84
num_new = num_int + num_float
print("datatype of num_int:",type(num_int))
print("datatype of num_float:",type(num_float))
print("Value of num_new:",num_new)
print("datatype of num_new:",type(num_new))
#OutPut Will be
datatype of num_int: <class 'int'>
datatype of num_float: <class 'float'>
Value of num_new: 322.84
datatype of num_new: <class 'float'>
In the preceding program,
We include two factors num_int and num_float, preserving the value in num_new.
We’ll have a look at the data type of three items respectively.
From the output, we may observe the datatype of num_int is an integer, the datatype of num_float is a float.
Additionally, we may observe the num_new has float data type as Python consistently transforms smaller data type to bigger data type to prevent the lack of data.
What is Typecasting in Python?
Convert one data type to another data type is called typecasting. Some situations, when you want to convert the data type.
For example, you want to add two numbers in which one existing value of a variable is an integer and the second is a string.
Then you need to use Python typecasting to convert string data type in integer before adding.
There are some functions which used for Python type casting:
int(): is used to specify integer literal. Â Construct an integer number from a string literal and Float.
str(): is used to specify the string literal. Construct a string type from an integer literal OR float literal.
float(): is used to specify the float data types. Construct float literal from integer and string literal.
Let us take an example to understand this logic.
Python Type Casting Logic
Here you can get a simple Logic to convert one Data Type to another Data Type. There is no rocket science, simply you can follow the simple steps as given below.
Python Cast to Float
Python cast to Float: Need to convert the Integer and String Data Type into Float.
As you can observe three types of casting that construct value according to the casting. Pythonic is an easy language that has many predefined functions to solve these types of problems.
Let us take a more real-life example to understand the type of casting and their usage in the real-life problem.
For example, we want to add two numbers in which one variable has the string value and the second variable has an integer value.
How is it possible to add? we can solve these problems by using Python typecasting. Let see an example program
How to add two numbers in which one variable has the string value and second has integer?
# first we can convert an integer to add
z=int(y) # here int() function is used for casting
Note: String value cannot be added with the integer value. Therefore, it is not possible to add without Typecasting. In the above example, first, we can convert the string value into an integer value. Then we can add.
I hope this article is very helpful to understand “Typecasting in Python | Python type Conversion“.