Typecasting in Python – Changing Data Types in Python

BySlide Scope

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

>>>type(X)

<class ‘str’>

>>> print(int(X))

15 # X is now integer

 

About the author

Slide Scope administrator

Slide Scope Posts are written by experts from Core Information Technology and Digital Marketing Industry. We have assistants from Management Science as well.

Leave a Reply