TypeCasting in Python | Explained With Examples

Typecasting” simply means type conversion. In Python, different predefined functions are used to convert any variable data type into another data type. Python is a trendy language among developers as it provides multiple functions and libraries that make complex tasks solved very easily. Python is an interpreted language, so it is not compulsory to define a data type. Python automatically detects the input variable data type and performs operations on it.

This Python post will provide a comprehensive guide on type casting and how it works in Python. The content of this guide is as follows:

Let’s get started!

Implicit Type Casting

The implicit type method in Python automatically converts a variable data type into another variable type. This method does not need any instruction from the user as it automatically detects the data type and converts it to another appropriate data type.

Let’s understand the implicit type casting with the example of code given below:

Example 1: Implicit Type Casting of int Value Into Float

In the example, the integer data type will be converted to float implicitly.

Code

# implicit type Casting Example

value_1 = 7
value_2 = 3.0
value_3 = value_1 + value_2
value_4 = value_1 * value_2

print(type(value_1))

print(type(value_2))

print(value_3)
print(type(value_3))

print(value_4)
print(type(value_4))

In the above code:

  • Two variables named “value_1” and “value_2” are initialized directly.
  • Variables named “value_3” and “value 4” performs addition and multiplication to the variable named “value_1” and “value_2”.
  • To check the data type of all variables, the “type()” function is used inside the parentheses of the print statement.

Note: The final output value after addition and multiplication is the “float” value because when one of the operand values is in float type, the final value always returns in float data type.

Output

The output shows the data type of all input variables.

Example 2: Implicit Type conversion while division

Let’s understand the implicit type casting with another example of code given below:

Code

value_1 = 12
value_2 = 5
div = value_1/value_2
print("Data type of first value: ", type(value_1))
print("Data type of second value: ", type(value_2))
print("Data type after division: ", type(div))
print("Value of division: ", div)

In the above code:

  • Two integer variable values are initialized.
  • Two variable values are divided using the operator symbols “/” and stored in the variable named “div”.
  • Print statement prints data type of input variable and division variable.
  • The final output after the division of integer numbers is in the “float”. This is because Python does not want to prevent the decimal part of our data from being lost, so the integer value is converted into float type.

Output

The output shows the conversion of the two “int” value numbers into the “float” data type.

Explicit Typecasting

In this method, the user provides the pre-defined function such as “float()”, “str()”, “int()”, etc to convert the data type into the required data type.

Let’s typecast some data type into needed data type with multiples example given below:

Example 1: Typecasting int value into float

This example demonstrates the method to convert int to float via explicit type casting in Python:

Code

# Explicit type Casting Example

#typecasting int value into float
value_1 = 5
new_value = float(value_1)
print(new_value,type(new_value))

In the above code:

  • The int value is initialized in a variable named “value_1”.
  • The “float()” function takes the “int” variable value as an argument and prints the result in the float data type.
  • The “type()” function is used to print the data type of the final variable named “new_value”.

Output

In the above output, the int value “5” is typecast into float value “5.0”.

Example 2: Typecasting string value into int

This explicit type casting example demonstrates the conversion of a string into an int. Let’s see how it works:

Code

# Explicit type Casting Example
value_1 = "5"

#typecasting string value into int
new_value = int(value_1)
print(new_value,type(new_value))

In the above code:

  • The string variable named “value_1” is initialized.
  • The “int” function takes string variable value as a parameter value and returns the integer value.
  • The “type()”function is used to print the data type of the final value stored in a variable named “new value”.

Output

In the above output string value “5” is type cast into an integer value.

That’s it from this guide!

Conclusion

In Python, “Typecasting” converts the variable type into another data type by using a pre-defined function such as “int()”, “str()” “float()”, etc. The “implicit” type casting does not need any function to convert the data type, as it automatically converts the data type according to the operation. In the “explicit” typecasting, the user converts any data type according to his requirement using the data type function. This article has covered all the type casting types in Python with numerous examples.

Over 500 Million Views on Our Blogs and YouTube! We’re inviting 10 businesses to partner with us and grow together. Click here to join us! ×