Easy Tutorial
❮ Python Date Time Python Func Number Atan ❯

Python3 Data Type Conversion

Sometimes, we need to convert the built-in types of data. In general, you just need to use the data type as the function name for conversion.

Python data type conversion can be divided into two types:

Implicit Type Conversion

In implicit type conversion, Python automatically converts one data type to another without any user involvement.

In the following example, we perform an operation on two different types of data, and the lower data type (integer) is converted to the higher data type (float) to avoid data loss.

Example

num_int = 123
num_flo = 1.23

num_new = num_int + num_flo

print("datatype of num_int:", type(num_int))
print("datatype of num_flo:", type(num_flo))

print("Value of num_new:", num_new)
print("datatype of num_new:", type(num_new))

The output of the above example is:

datatype of num_int: <class 'int'>
datatype of num_flo: <class 'float'>
Value of num_new: 124.23
datatype of num_new: <class 'float'>

Code Analysis:

Let's look at another example where an integer and a string are added:

Example

num_int = 123
num_str = "456"

print("Data type of num_int:", type(num_int))
print("Data type of num_str:", type(num_str))

print(num_int + num_str)

The output of the above example is:

Data type of num_int: <class 'int'>
Data type of num_str: <class 'str'>
Traceback (most recent call last):
  File "/tutorialpro-test/test.py", line 7, in <module>
    print(num_int + num_str)
TypeError: unsupported operand type(s) for +: 'int' and 'str'

From the output, we can see that the operation between an integer and a string results in an error, outputting a TypeError. Python cannot use implicit conversion in this case.

However, Python provides a solution for these types of situations, called explicit conversion.

Explicit Type Conversion

In explicit type conversion, the user converts the object's data type to the desired data type. We use predefined functions like int(), float(), str() to perform explicit type conversion.

int() converts to integer:

Example

x = int(1)   # x will be 1
y = int(2.8) # y will be 2
z = int("3") # z will be 3

float() converts to float:

Example

x = float(1)     # x will be 1.0
y = float(2.8)   # y will be 2.8
z = float("3")   # z will be 3.0
w = float("4.2") # w will be 4.2

str() converts to string:

Example

x = str("s1") # x will be 's1'
y = str(2)    # y will be '2'
z = str(3.0)  # z will be '3.0'

To perform operations between an integer and a string, we can use type casting:

Example

num_int = 123
num_str = "456"

print("Data type of num_int:", type(num_int))
print("Data type of num_str before conversion:", type(num_str))

num_str = int(num_str)  # Cast to integer
print("Data type of num_str after conversion:", type(num_str))
num_sum = num_int + num_str

print("The result of adding num_int and num_str is:", num_sum)
print("The data type of sum is:", type(num_sum))

The above example outputs:

The data type of num_int is: &lt;class 'int'>
Before type conversion, the data type of num_str is: &lt;class 'str'>
After type conversion, the data type of num_str is: &lt;class 'int'>
The result of adding num_int and num_str is: 579
The data type of sum is: &lt;class 'int'>

Several built-in functions can perform conversions between data types. These functions return a new object representing the converted value.

Function Description
int(x [,base]) Converts x to an integer
float(x) Converts x to a floating-point number
complex(real [,imag]) Creates a complex number
str(x) Converts object x to a string
repr(x) Converts object x to an expression string
eval(str) Evaluates a string containing a valid Python expression and returns an object
tuple(s) Converts the sequence s to a tuple
list(s) Converts the sequence s to a list
set(s) Converts to a mutable set
dict(d) Creates a dictionary. d must be a sequence of (key, value) tuples.
frozenset(s) Converts to an immutable set
chr(x) Converts an integer to a character
ord(x) Converts a character to its integer value
hex(x) Converts an integer to a hexadecimal string
oct(x) Converts an integer to an octal string
❮ Python Date Time Python Func Number Atan ❯