Casting in Python Explained
Getting Started
At times, we may need to convert a data type into another specific one. In Python, the process is known as casting, or usually referred to as type conversion. Python provides several built-in functions for type casting, including int()
, float()
, and str()
. These functions work by taking a value and converting it to the specified data type.
Implicit vs. Explicit Type Conversion
In Python, type conversion can be either implicit or explicit. Implicit type conversion occurs automatically when you perform operations between variables of different types. For example, when you add an integer and a float, Python will implicitly convert the integer to a float before performing the addition.
x = 10
y = 20.5
z = x + y
print(z) # 30.5
print(type(z)) # <class 'float'>
We notice that the variable x
is an integer, while the variable y
is a float. When we perform the addition, Python implicitly converts the integer to a float so that the operation is done smoothly. The result of the operation is then stored in the variable z
as a float.
In contrast, explicit type conversion happens when we use the built-in type casting functions. We'll go through several interesting examples to showcase the difference.
Type Casting a String to an Integer and Float
Observe how we can cast strings to these numeric types.
# Convert a string to an integer
x = "10"
y = int(x)
print(type(y)) # <class 'str'>
print(y) # 10
# Convert a string to a float
x = "10.5"
y = float(x)
print(type(y)) # <class 'float'>
print(y) # 10.5
Type Casting an Integer to a Float and String
Additionally, we can cast integers to both floats and strings.
# Convert an integer to a float
x = 50
y = float(x)
print(y) # 50.0
print(type(y)) # <class 'float'>
# Convert an integer to a string
x = 75
y = str(x)
print(y) # "75"
print(type(y)) # <class 'str'>
Type Casting a Float to an Integer and String
We can do the same with floating-point numbers.
# Convert a float to an integer
x = 25.8
y = int(x)
print(y) # 25
print(type(y)) # <class 'int'>
# Convert a float to a string
x = 33.3
y = str(x)
print(y) # "33.3"
print(type(y)) # <class 'str'>
Casting Caveats
While casting can be a useful tool for converting data in Python, we should be mindful and aware of some of the pitfalls that can arise. One common problem is loss of precision when casting a float to an integer, as the decimal portion of the float is truncated. This is exactly the case in our first example from the previous section.
So, to improve the precision in the conversion we'll use the round()
function to round the float to a specified number of decimal places before casting it to an integer.
x = 25.8
y = int(round(x))
print(y) # 26
print(type(y)) # <class 'int'>
Final Thoughts
While this information should serve as a solid foundation, there's always more to learn and explore in Python.
For instance, we can leverage the struct
module to perform binary packing and unpacking of data, allowing us to convert between binary data and Python data types.
Advanced techniques with the NumPy library enable us to carry out array casting and operations, oftentimes useful for scientific computing and data analysis.
Naturally, all of this shouldn't stop you from using what you learned so far. For more information on casting in Python, be sure to check out the following resources, and also take a look at our article covering the basics of numbers in Python.