Typecasting in python: Types and Examples

Typecasting is a concept in python that helps us to convert any particular variable datatype into a different data type to execute the performed operations. Read on to know the types of typecasting with provided examples below.

There are two ways to perform typecasting in python.

  1. Implicit Type Casting
  2. Explicit Type Casting

Refer to this to know how to take a user input in python before learning to typecast.

Implicit Type Casting

This method in python can convert a certain data type into another data type without involving the user in the process. A process to convert data types automatically is called Implicit Type Casting

# Python program to demonstrate implicit type Casting
  
 # Python automatically converts
 # a to int
 a = 7
 print(type(a))
  
 # Python automatically converts
 # b to float
 b = 3.0
 print(type(b))
  
 # Python automatically converts
 # c to float as it is a float addition
 c = a + b
 print(c)
 print(type(c))
  
 # Python automatically converts
 # d to float as it is a float multiplication
 d = a * b
 print(d)
 print(type(d)) 
<class 'int'>
 <class 'float'>
 10.0
 <class 'float'>
 21.0
 <class 'float'> 

Explicit Type Casting

To perform certain operations, python needs a user to convert the data types into particular data types required.

Typecasting can be done with data types like Int(), float(), and str(). These functions take the string as an argument and convert it into any of their respective data types.

For example, the int() function can take float and string as an argument and return the int type as an object.

Type Casting Examples Shown Below

Typecasting Integer int() to float

An example is to convert the integer data type into a floating-point.

# Python program to demonstrate
# Type Casting
  
# int variable
 a = 5
  
 # typecast to float
 n = float(a)
  
 print(n)
 print(type(n)) 
5.0
<class 'float'>

Typecasting float to integer int()

Using the int() function to convert float data type to integer data type.

# Python program to demonstrate
 # Type Casting
  
 # int variable
 a = 5.9
  
 # typecast to int
 n = int(a)
  
 print(n)
 print(type(n)) 
5
<class 'int'>

Typecasting integer (int) to string.

Here we can cast the integer data type into a string data type by using the str() function.

# Python program to demonstrate
# Type Casting
  
 # int variable
 a = 5
  
 # typecast to str
 n = str(a)
  
 print(n)
 print(type(n)) 
5
<class 'str'>

Typecasting string to integer int()

Converting the string to an integer data type by using the int() function

# Python program to demonstrate
# Type Casting
  
# string variable
 a = "5"
  
# typecast to int
 n = int(a)
  
 print(n)
 print(type(n)) 
5
<class 'int'>

Typecasting a string to float

Here, we’ve used a float() function to typecast a string into a float datatype.

# Python program to demonstrate
# Type Casting
  
 # string variable
 a = "5.9"
  
 # typecast to float
 n = float(a)
  
 print(n)
 print(type(n)) 
5.9
<class 'float'>

We hope that the above examples helped you to level up your python journey. You can continue to learn next: how to return multiple values in python.

We will be happy to hear your thoughts

      Leave a reply

      Techs Tricks
      Logo
      Reset Password
      Shopping cart