Junior — Middle
How is data type conversion performed in Python?
sobes.tech AI
Answer from AI
In Python, type casting is performed using built-in functions that explicitly convert a value from one type to another. For example:
int()— converts to an integerfloat()— to a floating-point numberstr()— to a stringbool()— to a boolean type
Example:
x = "123" # string
num = int(x) # now num is the integer 123
Python also supports implicit type conversion in some operations, for example, when adding an integer and a floating-point number, the result will be float. However, for explicit control, it is better to use conversion functions.