Typecasting in Python – Changing Data Types in Python

Typecasting in Python

It is the process of changing one datatype to another data type.

Functions used to do typecasting.

str(x) – To change x to string

int(x) – To change x to integer ( x must be float or bool but not complex or string)

float(x) – To change x to float ( x must be int or boolean)

complex(x) – To change x to complex number. ( x must be numeric or bool)

bool(x) – To change values to True or False ( 1 is true and 0 is False )

Here x is the variable which has to be typecasted.

>>>X = “15” # Here x is string


<class ‘str’>

>>> print(int(X))

15 # X is now integer

Leave a Comment