In Python, we can convert one type of variable to another type. This conversion is called type casting or type conversion.
In casting, we convert variables declared in specific data types to the different data types.
Python performs the following two types of casting.
- Implicit casting: The Python interpreter automatically performs an implicit Type conversion, which avoids loss of data.
- Explicit casting: The explicit type conversion is performed by the user using built-in functions.
To perform a type casting, we are going to use the following built-in functions
int()
: convert any type variable to the integer type.float()
: convert any type variable to the float type.complex()
: convert any type variable to the complex type.bool()
: convert any type variable to the bool type.str()
: convert any type variable to the string type.
In type casting, data loss may occur because we enforce the object to a specific data type.
Table of contents
Int type conversion
In int
type conversion, we use the int()
function to convert variables of other types to int
type. Variable can be of any type such as float
, string
, bool
.
While performing int
type conversion, we need to remember the following points.
- When converting string type to int type, a string must contain integral value only and should be base-10.
- We can convert any type to
int
type, but we cannot perform complex to int type.
Casting float value to an integer
Casting Boolean value to an integer
Casting a string to an integer
When converting string type to int type, a string must contain integral value only and should be base-10. If you try to convert
Example
Float type conversion
In float type conversion we use a built-in function float()
. This function converts variables of other types to float
types.
Casting integer to float
Casting Boolean to float
Casting string to float
While performing float type conversion, we need to remember some points.
- We can convert any type to
float
type, but we cannot cast complex to float type. - While converting string type to float type, a string must contain an integer/decimal value of base-10.
Complex type conversion
In complex
type conversion, we use the built-in function complex()
to convert values from other types to the complex type. Value can be any type including of int
, float
, bool
, str
.
The complex function has the following two forms for conversion.
complex(x)
: To convert a valuex
into acomplex
type. In this form, the real value isx
, and the imaginary value is 0.complex(x, y)
: To convert the valuex
andy
into acomplex
type. In this form, the real value isx
, and the imaginary isy
.
Casting integer type to complex type
Casting float type to complex type
Casting Boolean type to complex type
Bool type conversion
We use the built-in function bool()
to convert values of other types to bool
types. This function returns two values, either True
or False
.
- We can convert any type of values to bool type, and the output for all values will be
True
, Except 0, which is False. - If you convert an empty string to a boolean it will be converted to boolean False.
The bool True
is 1 and False
is 0. Every non-zero value is treated as True
.
Casting integer to Boolean type
Casting float to Boolean type
Casting string to Boolean type
Casting complex type to Boolean type
String type conversion
In str
type conversion, we use the built-in function str()
to convert converts variables of other types to a string type. This function returns the string type of object (value).