free stats

Python Type Casting

Last Update : 20 Sep, 2022 Python, Programming

In this tutorial, You will learn about Python type casting and its usage.

Type Casting is the method of converting one data type into a specific data type to do the operation required to be performed by users.

As an example, a programmer needs to convert an integer to a string in order to display an integer as part of a message. This process is called type casting in programming.

There are two types of type casting available in Python programming.

  • Implicit Type Casting
  • Explicit Type Casting

 

Python Implicit Type Casting

Python converts one data type into another data type automatically in implicit type casting. Also, users do not need to involve in this process.

For example -:

# Python automatically converts
# 'shape_length' variable value to int
shape_length = 22
print(type(shape_length))
 
# Python automatically converts 
# 'shape_width' variable value to float
shape_width = 23.4
print(type(shape_width))
 
# Python automatically converts
# 'area' variable value to float as it is a multiplication of the float
area = shape_length * shape_width
print(area)
print(type(area))

This program produces the following result -:

<class 'int'>
<class 'float'>
514.8
<class 'float'>

 

Python Explicit Type Casting

In explicit type casting, the Python programmers require to involve in converting one data type into another certain data type to do the operation required.

Python is an object-orientated programming language, and as such Python uses classes to define data types, including the primitive types of Python. Therefore, Python type casting is done by using constructor functions.

Python type casting can be done using the following three data type functions.

  •  int() -: This function takes a float or a string object as an argument and returns int type object.
  • float() -: This function takes an int or a string as an argument and returns float type object.
  • str() -: This function takes a float or an int as an argument and returns string type object.

The following shows some examples of Python type casting.

 

int to float Type Casting

This example shows casting integer object to float object using float() function.

# integer variable
x = 25
 
# type casting to float
y = float(x)
 
print(y)
print(type(y))

This program produces the following result -:

25.0
<class 'float'>

 

float to int Type Casting

This example shows casting float object to integer object using int() function.

# float variable
x = 25.3
 
# type casting to int
y = int(x)
 
print(y)
print(type(y))

This program produces the following result -:

25
<class 'int'>

 

int to string Type Casting

This example shows casting int object into string object using str() function.

# int variable
x = 25
 
# type casting to str
y = str(x)
 
print(y)
print(type(y))

This program produces the following result -:

25
<class 'str'>

 

string to int Type Casting

This example shows casting string object into integer object using int() function.

# string variable
x = "25"
 
# type casting to int
y = int(x)
 
print(y)
print(type(y))

This program produces the following result -:

25
<class 'int'>

 

string to float Type Casting

This example shows casting string object into float object using float() function.

# string variable
x = '25.9'
 
# type casting to float
y = float(x)
 
print(y)
print(type(y))

This program produces the following result -:

25.9
<class 'float'>

 

Summary

  • Type Casting is the method of converting one data type into a specific data type.
  • Python type casting can be Implicit Type Casting or Explicit Type Casting.
  • Converts one data type into another data type automatically in implicit type casting.
  • Python use int(), float() and str() function in the Explicit Type Casting.

 

You found this tutorial / article valuable? Need to show your appreciation? Here are some options:

01. Spread the word! Use following buttons to share this tutorial / article link on your favorite social media sites.

02. Follow us on Twitter, GitHub ,and Facebook.