What is Type Casting?
Sometimes you need to convert a value from one data type to another.
This is called type casting or type conversion.
For example, you might have a number stored as a string and need to convert it into an integer before doing a calculation.
There are two ways type conversion can happen in Python:
- Explicit Type Conversion
- Implicit Type Conversion
Explicit Type Conversion
When you manually convert one data type into another, it is called explicit type conversion.
Python provides built-in functions such as:
int()- converts a value to an integerfloat()- converts a value to a floatstr()- converts a value to a stringbool()- converts a value to a boolean
For example:
age = "27"
age = int(age)
print(age)
print(type(age))Here, "27" is a string.
Using int(), we convert it into an integer.
27
<class 'int'>You can also convert other values:
x = 10
print(float(x))
print(str(x))
print(bool(x))The result will be:
10.0
10
TrueThe important thing to remember is that you are telling Python to convert the value.
bool() and True/False Values
bool() converts a value to either True or False.
Most values become True. Values that represent zero or emptiness become False, such as:
print(bool(0))
print(bool(""))
print(bool([]))
print(bool(None))
# Output:
# False
# False
# False
# FalseFor example:
print(bool(10))
print(bool("Hello"))
# Output:
# True
# Trueint() Does Not Round
One small thing to remember: int() cuts off the decimal part instead of rounding the number.
print(int(9.8))
# Output:
# 9So int(9.8) gives 9, not 10.
Not Every Conversion Works
Some conversions are not possible.
For example:
x = "hello"
print(int(x))This gives an error because "hello" cannot be converted into an integer.
So, type conversion doesn’t mean Python can convert every value into every type.
Implicit Type Conversion
Sometimes, Python converts a value automatically when performing an operation with different data types.
For example:
x = 10
y = 2.5
result = x + y
print(result)
print(type(result))
# Output:
# 12.5
# <class 'float'>Here, x is an integer (int) and y is a float (float).
Python automatically converts the integer to a float so the calculation can be performed.
You don’t need to call float() yourself.
This is called implicit type conversion.
Explicit vs Implicit
The difference is simple:
Explicit conversion: You convert the value yourself.
x = "10"
x = int(x)Implicit conversion: Python converts the value automatically.
x = 10
y = 2.5
result = x + yA Small Thing to Remember
Type conversion doesn’t always create the result you might expect.
For example:
x = 10
y = "20"
print(x + y)This gives an error because Python won’t automatically convert the string "20" into the number 20.
You need to convert it yourself:
print(x + int(y))
# Output:
# 30That’s why understanding data types is important when working with Python.
Quick Recap
- Type casting means converting one data type into another.
- Explicit conversion is done manually using functions like
int(),float(),str(), andbool(). - Implicit conversion happens automatically when Python can safely convert a value during an operation.
bool()converts values toTrueorFalse.int()cuts off the decimal part instead of rounding it.- Not every type can be converted into every other type.
👉 Next tutorial: Python Input Function