In this section we will learn what type casting is and how to use it in Python.
What is Type Conversion in Python? (Python Casting)
The process of converting a value with one data type to another value with different data type is called type casting or type conversion.
Types of Casting in Python
In Python there are two types of casting:
-
Implicit Casting
-
Explicit Casting
Implicit Casting in Python
Implicit casting is a type of casting that happens automatically and without the involvement of developers!
For example, let’s say you have an integer value and a float value. Now you want to add these two values together and print the result.
In an operation like addition, both involved operands should be of the same type! For example, this means either both data types should be integer or be float.
In Python, implicit casting always happens when a value of a data type with lower memory space is converted to the data type with higher memory space.
So here the Float data type has higher memory space compared to the integer data type. And for this reason the value of int data type in our example will be converted to float data type so that it matches the other operand.
That’s how implicit casting works in Python.
Example: implicit casting in python
val1 = 20
val2 = 45.43
res = val2 + val1
print(res)
Output:
65.43
Explicit Casting in Python
On the other hand, explicit casting is when developers explicitly request so that a value of a data-type to be converted into another value with different data type!
For example, let’s say we have a string value like “23”. As you can see, this is a string value but the content is two digital number! So we might want to involve this value in an operation where integer values are needed. Now if we pass this string value directly to such operation, the result would be an error because even though the content is decimal numbers, but the data type is string.
So in situation like this, Python provided a set of functions that we can use to apply explicit conversion to a value. For example we can use the int() function to explicitly convert a string value into an integer.
Note: for converting a string value into either int or float, we need to make sure the content of that string is actually number! Otherwise, running an explicit conversion on a value that does not contain numbers, would cause error instead!
Python convert Int to String: Python str() Function
The str() function takes an argument of other data types (like int, float etc.) and it will return the string representation of that value. Basically, it will take a copy of the value and convert it into a value that is of type string.
Example: using str() function in Python
val1 = 45.43
res = str(val1)
print(type(res))
Output:
<class 'str'>
As you can see, the type of res is now str which stands for string.
Python Convert String to Int: Python int() Function
Using the int() function we can convert other data types (like string, float etc.) into a value of int type.
Note: for floating values, using this function will cause the fraction part of the value to be dismissed. Also for string values, the content of that string should be an integer number! Otherwise an error will return instead.
Example: converting string to int in python
val1 = "45.43"
res = int(val1)
print(res)
Output:
ValueError: invalid literal for int() with base 10: '45.43'
Note that in this example it seems the value in the val1 is a string value with the content that is essentially a floating value! So it seems that explicit operation should work here but in reality though it won’t work! Because there’s a dot . in the content of the val1, the int() function will return an error instead.
We could call the float function on the val1 to first convert it into a float value and then call the int() function on top of the result to get the final int value.
Take a look at the example below:
val1 = "45.43"
res = float(val1)
intRes = int(res)
print(intRes)
Output:
45
Python Convert String to Float: Python float() Function
The float() function is used to convert other data types like string for example, into a value of float data type.
Note: when converting a string value into a float data type using this function, make sure the entire elements of that string value is actually number! Otherwise an error will return instead.
Example: using float() function in Python
val1 = "45.43"
res = float(val1)
print(res)
Output:
45.43
Python bool() Function
The bool() function is used to convert an object into a boolean value.
For example, any integer or floating value that we pass to this function, unless it’s not 0, the return value of this function will be True.
In short, the result of this function for any object we pass to it will be True unless:
The object is empty, like [], (), {}
The object is False
The object is 0
The object is None
For the exceptions mentioned above, the result of invoking this function will be False.
Example: using bool() function in Python
print(bool(40))
print(bool(32.22))
print(bool(0))
print(bool("False"))
print(bool(""))
print(bool({}))
print(bool([]))
Output:
True
True
False
True
False
False
False