Type Casting in Python
Type casting (also called type conversion) is the process of converting one data type into another.
In Python, type casting can be:
- Implicit (automatic) — handled by Python itself when no data loss will occur.
- Explicit (manual) — done by the programmer using type conversion functions.
Common Type Conversion Functions
| Function | Description |
|---|---|
int(x) | Converts to integer |
float(x) | Converts to float |
str(x) | Converts to string |
bool(x) | Converts to boolean |
list(x) | Converts to list |
tuple(x) | Converts to tuple |
set(x) | Converts to set |
Example 1: Integer Conversion (int())
typecasting_int.py
x = 5.8
print(int(x)) # Float to int
y = "42"
print(int(y)) # String to int
z = True
print(int(z)) # Bool to intoutput.txt
5
42
1Explanation:
- Converts float to integer (drops decimal).
- Converts numeric string to integer.
- Converts boolean:
True→1,False→0.
Example 2: Float Conversion (float())
typecasting_float.py
x = 7
print(float(x)) # Int to float
y = "3.14"
print(float(y)) # String to float
z = False
print(float(z)) # Bool to floatoutput.txt
7.0
3.14
0.0Explanation:
- Converts integer to float.
- Converts numeric string to float.
- Converts boolean to float.
Example 3: String Conversion (str())
typecasting_str.py
x = 25
print(str(x)) # Int to string
y = 8.5
print(str(y)) # Float to string
z = None
print(str(z)) # None to stringoutput.txt
25
8.5
NoneExplanation:
- Converts any object to a string.
- Useful for concatenation or display.
Example 4: Boolean Conversion (bool())
typecasting_bool.py
print(bool(0)) # Zero → False
print(bool(42)) # Non-zero → True
print(bool("")) # Empty string → False
print(bool("Hello")) # Non-empty string → Trueoutput.txt
False
True
False
TrueExplanation:
0, empty string,None, empty containers →False.- All other values →
True.
Example 5: List Conversion (list())
typecasting_list.py
x = "abc"
print(list(x)) # String to list of characters
y = (1, 2, 3)
print(list(y)) # Tuple to listoutput.txt
['a', 'b', 'c']
[1, 2, 3]Explanation:
- Converts iterable (string, tuple) into list.
- Each element becomes a list item.
Example 6: Tuple Conversion (tuple())
typecasting_tuple.py
x = [10, 20, 30]
print(tuple(x)) # List to tuple
y = "xyz"
print(tuple(y)) # String to tupleoutput.txt
(10, 20, 30)
('x', 'y', 'z')Explanation:
- Converts iterable into tuple.
Example 7: Set Conversion (set())
typecasting_set.py
x = [1, 2, 2, 3]
print(set(x)) # List to set (removes duplicates)
y = "aab"
print(set(y)) # String to set of unique charactersoutput.txt
{1, 2, 3}
{'a', 'b'}Explanation:
- Converts iterable into set.
- Removes duplicate values.
Implicit Type Casting
Python also automatically converts types where possible.
implicit_typecasting.py
x = 5
y = 2.5
result = x + y
print(result)
print(type(result))output.txt
7.5
<class 'float'>Explanation:
- Python promotes
inttofloatautomatically to avoid data loss.
Summary
- Explicit Type Casting is done using functions like
int(),float(),str(), etc. - Implicit Type Casting is performed automatically when safe.
- Type casting is very useful for user input, arithmetic, string formatting, and data processing.