Changing the data type of a variable in Python from one type to another , as and when required, is called type conversion.
Type conversion is of two types:
- Explicit Conversion
- Implicit Conversion
Explicit Conversion
Explicit conversion, also called type casting , happens when data type conversion takes place because the programmer forced it in the program.
The general form of an explicit data type conversion is:
(new_data_type) (expression)
Examples:
Function |
Description |
int(x) |
Converts x to an integer |
float(x) |
Converts x to a floating point number |
str(x) |
Converts x to a floating point representation |
chr(x) |
Converts ASCII value of x to character |
Implicit Conversion
Implicit conversion, also known as coercion, happens when data type conversion is done automatically by Python and is not instructed by the programmer.
Example: