Python 3 – How to Typecast to string, int, float, complex ?

In Python, we can convert the type data values to multiple types using the typecasting functions as shown below:

List of Typecasting Functions in Python:

  • str() : Convert value to string type.
  • int() : Convert value to integer type.
  • float() : Convert value to float type.
  • complex() : Convert value to complex type.

You can use the above function to type case variable data type into in other:

a = 1 # int
b = 3.8 # float
c = True # bool
d = 12 # int
print(str(float(a))+ " Converting int to float")
print(str(int(b))+ " Converting float to int")
print(str(complex(a))+ " Converting int to complex")
print(str(c)+ " Converting bool to string")

#OUTPUT#

1.0 Converting int to float
3 Converting float to int
(1+0j) Converting int to complex
True Converting bool to string

 

Leave a Comment

Your email address will not be published. Required fields are marked *