Type Casting In Python
Last updated
Last updated
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 more simple terms, type casting means converting operand's data type to another data type.
There are 4 important Functions used for Data Type Conversion:
int() - int() converts any data type to integer data type.
float() - float() converts any data to float data type.
str() - str() converts any data type to string data type
bool() - bool() converts any data type to boolean data type
Converting float value in integer:
In [13]:
Converting boolean value in integer:
In [2]:
In [3]:
Note:Converting boolean value into integer will give 1 if boolean is True and 0 if false. In more simpler terms True means 1 and False means 0.
Converting string value in integer:
Note:String can only be converted in integer , if it is possible mathematically or if the string is a number. This means that string "ConsoleFlare" can not be converted to integer data type as it is not possible, but string "123" can be converted to 123.See Below:
Characters as a String: Will not convert
In [4]:
Integer as a String: Will convert
In [5]:
Converting integer data type in float:
In [6]:
Converting boolean data type in float:
In [8]:
Converting String data type in float:
Note:String can only be converted in float , if it is possible mathematically or if the string is a number. This means that string "ConsoleFlare" can not be converted to integer data type as it is not possible, but string "123.5" can be converted to 123.5.See Below:In [14]:
Note:Can we convert a String Float (float number in string) in integer data type directly?In [15]:
This will give an error, We cannot convert String float Directly into an integer data type. But there is a WorkAround that we can follow.
Convert string float to float data type.
Convert the float value to integer.
In [16]:
Note:str() can convert data type of any variables or values to string data type with no obstacles and no workaround needed.In [17]:
To Convert Data type of a particular operand in boolean , we must know what that particular value represents , does it represent 0(False) or 1(True).
While converting any value in boolean , outcome will be either non-zero value which is True or zero-value which is false.
Zero-Values: Zero-values are the values that is equal to nothing or 0. Because 0 in its total sense is nothing.
Zero-values: 0 , "" , '','''''' and more where the value is nothing.
Non Zero-Values: Non Zero-values are the values that is not equal nothing. Any value that is not zero or nothing is Non-Zero Value.
Non - Zero-values: "0",123,123.5," "(there is a space between quotes), "12321","ConsoleFlare"
In [18]:
In [19]:
In [20]:
In [21]:
In [22]:
In [23]:
In [ ]:
Note:You must have seen in examples that converting 0 (integer type) gives False, but converting "0" gives True. It's because string type 0 is not equal to 0.
Sometimes we need to do a particular operation on values but their data type does not allow us to perform such operations, in that case we need to convert its data type to perform desired operation.