python tutorial
Python Casting
Learn more about Python, one of the world’s most versatile and popular programming languages.
Type casting or Type conversion in Python allows casting or conversion of one value into another. At large, type casting or conversion is divided into two parts:
- Implicit Type Casting/Conversion
- Explicit Type Casting/Conversion
Let’s explore each type of casting in detail.
Implicit Type Casting/Conversion in Python
Implicit type casting or conversion is done by Python Interpreter. Some key points to keep in mind about implicit type casting or conversion are:
- Casting or Conversion of type is done by Python Interpreter automatically
- Python Interpreter will only do implicit conversion or casting when there is no loss of data
- Python Interpreter will only do implicit conversion or casting when both the types involved are compatible for casting.
Let’s take a look at a few examples of implicit type conversion or casting. In the example below, we have two variables one of type integer and another of type float. Float is considered to be a higher data type as it allows more range of values compared to an integer type. Hence when we try to add two numbers, each one of being type float and integer, the resulting number is always of higher data type – in this case float. This is the case so that there is no loss of data or information as integer values can still be accommodated into float types.
Note: we are using type()
method to check the data type of a given variable
# num_one of type integer
num_one = 50
# num_two of type float
num_two = 150.75
print(type(num one))
print(type(num_two))
<class 'int'>
<class 'float'>
# add 'num_one' of type int to 'num two' of type float
result = num_one + num_two
print(result)
print(type(result))
200.75
<class 'float'>
In this case, both variables involved were numbers and implicit type casting was possible. However, sometimes there is a need to explicitly cast values between incompatible data types like strings and numbers. Python Interpreter cannot implicitly convert values and will throw an error as shown below.
# num_one of type integer
nun_one = 50
# num_two of type string
num_two = "150.75"
print(type(num_one))
print(type(num_two))
<class 'int'>
<class 'str'>
# add 'num_one' of type int to 'num_two' of type string
result = num_one + num_two
print(result)
print(type(result))
TypeError Traceback (most recent call last)
<ipython-input-4-248a29f14f2b> in <module>
1 # add 'num_one' of type int to 'num_two' of type float
----> 2 result = num_one + num_two
3
4 print(result)
5 print(type(result))
TypeError: unsupported operand type(s) for +: 'int' and 'str'
In that case, we use explicit type conversion or casting.
Explicit Type Casting/Conversion in Python
Explicit casting is useful when Python Interpreter cannot implicitly convert between types for e.g. strings to numbers. In this we use built-in casting methods like int()
, float()
, str()
, etc. to convert one type to another.
In the example below, first we explicitly cast stringified number into float using float()
and then add the numbers.
# num_one of type integer
nun_one = 50
# num_two of type string
num_two_str = "150.75"
# num_two explicitly converted from string to number
num_two = float(num_two_str)
print(type(num one))
print(type(num_two))
<class 'int'>
<class 'float'>
# add 'num_one' of type int to 'num_two' of type string
result = num_one + num_two
print(result)
print(type(result))
200.75
<class 'float'>
Learn Python Today
Get hands-on experience writing code with interactive tutorials in our free online learning platform.
- Free and fun
- Designed for beginners
- No downloads or setup required