Skip to main content

Python Type Conversion and Type Casting

The process of converting the value of one data type (integer, string, float, etc.) to another data type is called * type conversion*.

Type Conversion

Python has two types of type conversion:

  1. Implicit Type Conversion
  2. Explicit Type Conversion

Implicit Type Conversion

In Implicit Type Conversion, a Python datatype is automatically converted to another datatype.

Example: Converting integer to float

For example, convert integer to float

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))

Output

datatype of num_int: <class 'int'>
datatype of num_flo: <class 'float'>

Value of num_new: 124.23
datatype of num_new: <class 'float'>

Example: Addition of string data type and integer datatype

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)
Data type of num_int: <class 'int'> 
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'
note

Thanks to type promotion it is possible to perform operations by converting data into a wider-sized data type without any loss of information.

For example, integer can be promoted to float, but string can't be promoted implicitly to integer. In the latter case, explicit conversion is required.

Explicit Type Conversion (Type Casting)

In Explicit Type Conversion in Python, the datatype is manually changed by the user to the required datatype.

This type of conversion is also called typecasting, because the user casts (changes) the data type of the objects.

warning

In Type Casting, data loss may occur as the object is imposed on a specific data type.

There are predefined functions to perform explicit type conversion:

FunctionDescription
int(a, base)Convert any datatype to integer. Base specifies the base in which string is if the data type is a string.
float(x)Convert x to a floating-point number.
ord(x)Convert a character x to integer.
hex(x)Convert integer x to hexadecimal string.
oct(x)Convert integer x to octal string.
tuple(x)Convert x to a tuple.
set(x)Convert x to a set
list(x)Convert x to a list
dict(x)Create a dictionary and x should be a sequence of (key, values) tuples
str(x)Convert x to a string
complex(real, imag)Create a complex number
chr(x)Convert integer x to a character

Some examples:

s = "100101"

# convert string to int using base 2
result = int(s, 2)
print("Result: ", result) # 37
print("Type: ", type(result)) # <class 'int'>

# convert string to float
result = float(s)
print("Result: ", result) # 100101.0
print("Type: ", type(result)) # <class 'float'>

# convert character to integer
result = ord('A')
print("Result: ", result) # 65
print("Type: ", type(result)) # <class 'int'>

# convert integer to hexadecimal string
result = hex(56)
print("Result: ", result) # 0x38
print("Type: ", type(result)) # <class 'str'>

# convert integer to octal string
result = oct(56)
print("Result: ", result) # 0o70
print("Type: ", type(result)) # <class 'str'>

word = "Hello"

# convert string to tuple
result = tuple(word)
print("Result: ", result) # ('H', 'e', 'l', 'l', 'o')
print("Type: ", type(result)) # <class 'tuple'>

# convert string to set
result = set(word)
print("Result: ", result) # {'o', 'H', 'l', 'e'}
print("Type: ", type(result)) # <class 'set'>

# convert string to list
result = list(word)
print("Result: ", result) # ['H', 'e', 'l', 'l', 'o']
print("Type: ", type(result)) # <class 'list'>

# convert integer to complex number
result = complex(1,2)
print("Result: ", result) # (1+2j)
print("Type: ", type(result)) # <class 'complex'>

# convert integer to string
result = str(123)
print("Result: ", result) # 123
print("Type: ", type(result)) # <class 'str'>

# convert tuple to dictionary
result = dict((('a', 1) ,('b', 2), ('c', 3)))
print("Result: ", result) # {'a': 1, 'b': 2, 'c': 3}
print("Type: ", type(result)) # <class 'dict'>