Python Type Casting

In python, type casting means converting the values from one data type to another data type like converting the integer (int) values to the string, string values to integer (int), etc. based on the requirements.

 

In python, the type conversion or type casting can be done by using the following built-in methods.

 

  • int()
  • float()
  • complex()
  • str()

The int(), float(), and complex() functions are useful to convert the numbers (int, float, and complex) from one type to another like int to float, float to int, int to complex, etc. and the str() function is useful to convert any object to string based on the requirements.

Python Numbers Conversion

Following is the example of converting the numbers (int, float, complex) from one type to another in python.

 

a = 10      #int

b = 31.54 #float

c = 3j       #complex

#convert from int to float

x = float(a)

#convert from float to int

y = int(b)

#convert from int to complex

z = complex(b)

print(x)

print(y)

print(z)

print("x type: ", type(x))

print("y type: ", type(y))

print("z type: ", type(z))

If you observe the above example, we are converting the variable values from one type to another based on our requirements.

 

When you execute the above python program, you will get the result like as shown below.

 

10.0

31

(31.54+0j)

x type:  <class 'float'>

y type:  <class 'int'>

z type:  <class 'complex'>

If you observe the above result, we converted numbers from int to float, float to int, int to complex types.

 

Here, you need to remember that it’s impossible to convert the complex numbers into another type in python.

Python String Conversion

In python, the built-in str() function is useful to convert any object such as numbers, list, etc. to a string.

 

Following is the example of converting int to string, float to string, and complex to string using the str() function in python.

 

x = str(10) #int to string

y = str(31.54) #float to string

z = str(3j) #complex to string

print("x type: ", type(x))

print("y type: ", type(y))

print("z type: ", type(z))

If you observe the above example, we are converting numbers (int, float, complex) to string using the str() function.

 

When you execute the above python program, you will get the result like as shown below.

 

x type:  <class 'str'>

y type:  <class 'str'>

z type:  <class 'str'>

Same way, you can also convert string to int, string to float, etc. by using int(), float(), etc. functions based on your requirements.

 

Following is the example of converting the string to int, float using int(), float() functions in python.

 

x = "10"

print(type(int(x)))

print(type(float(x)))

If you observe the above example, we are converting the string to int and float by using int(), float() function.

 

When you execute the above python program, you will get the result like as shown below.

 

<class 'int'>

<class 'float'>

This is how you can perform the type casting in python to change the value types from one data type to another data type based on your requirements.