English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية

Python Basic Tutorial

Python Flow Control

Python Functions

Python Data Types

Python File Operations

Python Objects and Classes

Python Date and Time

Advanced Knowledge of Python

Python Reference Manual

Python Type Conversion

Before learning about type conversion in Python, you should understand  Python Data Types.

Type Conversion

The process of converting the value of one data type (integer, string, floating-point number, etc.) to another data type is called type conversion. Python has two types of type conversions.

  1. Implicit Type Conversion

  2. Explicit Type Conversion

Implicit Type Conversion

In implicit type conversion, Python automatically converts one data type to another. This process does not require any user involvement.

Let's look at an example where Python promotes the conversion of lower data types (integers) to higher data types (floating-point numbers) to avoid data loss.

Example1: Convert integer to float type

num_int = 123
num_flo = 1.23
num_new = num_int + num_flo
print("num_int data type:", type(num_int))
print("The data type of num_flo:", type(num_flo))
print("The value of num_new:", num_new)
print("The data type of num_new:", type(num_new))

When we run the above program, the output will be:

num_int data type: <class 'int'>
The data type of num_flo: <class 'float'>
The value of num_new: 124.23
The data type of num_new: <class 'float'>

in the above program

  • We add two variablesnum_intandnum_flo additionand store the value in the variablein num_new.

  • we check the data types of all three objects separately.

  • In the output, we can seenum_intwhose data type is integer,num_flowhose data type is a float.

  • Additionally, we can seenum_newwith float data type because Python always converts the smaller data type to the larger data type to avoid data loss.

Now, let's try to add a string and an integer and see how Python handles it.

Example2: Addition of string (higher) data type and integer (lower) data type

num_int = 123
num_str = "456"
print("num_int data type:", type(num_int))
print("The data type of num_str:", type(num_str))
print(num_int+num_str)

When we run the above program, the output will be:

num_int data type: <class 'int'>
The data type of num_str: <class 'str'>
Traceback (most recent call last): 
  File "python", line 7, in <module> 
TypeError: unsupported operand type(s) for +: 'int' and 'str'

in the above program

  • we have two variablesnum_int andnum_straddition.

  • From the output, we can see that we got a TypeError. In this case, Python cannot use implicit conversion.

  • However, Python provides a solution for such cases called "Explicit Conversion".

Explicit Type Conversion

In "Explicit Type ConversionIn it, the user converts the data type of the object to the required data type. We use explicit type conversions with predefined functions like int(), float(), str(), and so on.

This type of conversion is also called type conversion because the user forces the conversion (change) of the object's data type.

Syntax :

<required_datatype>(expression)

Type conversion can be completed by assigning the required data type function to the expression.

Example3:Add string and integer using explicit conversion

num_int = 123
num_str = "456"
print("num_int data type:", type(num_int))
print("The data type of num_str before conversion:", type(num_str))
num_str = int(num_str)
print("The data type of the converted num_str:", type(num_str))
num_sum = num_int + num_str
print("num_int and num_str sum:", num_sum)
print("sum data type:", type(num_sum))

When we run the above program, the output will be:

num_int data type: <class 'int'>
The data type of num_str before conversion: <class 'str'>
The data type of the converted num_str: <class 'int'>
num_int and num_str sum: 579
sum data type: <class 'int'>

in the above program

  • Wenum_strandnum_intadd variables.

  • We use the int() function tonum_strfrom string (high) to integer (low) type to perform addition.

  • Convertnum_strAfter converting to integer, Python can perform addition on these two variables.

  • Finally, we getnum_sumValue and and the data type of the value is integer.

Points to remember

  1. Type conversion is the conversion of an object from one data type to another.

  2. Implicit type conversion is automatically executed by the Python interpreter.

  3. Python avoids data loss in implicit type conversion.

  4. Explicit type conversion is also known as type conversion, where users use predefined functions to convert the data type of an object.

  5. In type conversion, when we force an object to a specific data type, data loss may occur.