Type Casting means converting the data type of a value into another data type. Python supports both implicit and explicit type casting.
Python automatically converts one data type to another during operations:
x = 5 y = 2.0 z = x + y # x is automatically converted to float print(z) # Output: 7.0
You can manually convert data types using built-in functions:
a = int(3.9) # Converts float to int (Output: 3) b = float("4") # Converts string to float c = str(10) # Converts int to string print(a, b, c)
int()
– converts to integerfloat()
– converts to floatstr()
– converts to stringbool()
– converts to booleanlist()
, tuple()
, set()
– converts to list, tuple, setHelp others discover Technorank Learning by sharing your honest experience.
Your support inspires us to keep building!