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

Ankit Srivastava is an IT trainer, technology educator, and digital skills mentor with expertise in programming, data analytics, AI, and software development. He has successfully trained thousands of learners, with more than 10,000 student enrollments on Udemy. His practical teaching approach empowers students and professionals to build in-demand technical skills. Colorstech channel where Ankit posts video tutorials has more than 8000 Subscribers.


