Skip to content

Python Type Conversion and Type Casting

How to convert a binary string to an integer in Python using the int() function with base 2 parameter.

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'

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.

There are predefined functions to perform explicit type conversion:

Function Description
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"

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

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

result = ord('A')
print("Result: ", result)     # 65
print("Type: ", type(result)) # <class 'int'>
  
result = hex(56)
print("Result: ", result)     # 0x38
print("Type: ", type(result)) # <class 'str'>
  
result = oct(56)
print("Result: ", result)     # 0o70
print("Type: ", type(result)) # <class 'str'>

word = "Hello"

result = tuple(word)
print("Result: ", result)     # ('H', 'e', 'l', 'l', 'o')
print("Type: ", type(result)) # <class 'tuple'>
  
result = set(word)
print("Result: ", result)     # {'o', 'H', 'l', 'e'}
print("Type: ", type(result)) # <class 'set'>
  
result = list(word)
print("Result: ", result)     # ['H', 'e', 'l', 'l', 'o']
print("Type: ", type(result)) # <class 'list'>
  
result = complex(1,2)
print("Result: ", result)     # (1+2j)
print("Type: ", type(result)) # <class 'complex'>
  
result = str(123)
print("Result: ", result)     # 123
print("Type: ", type(result)) # <class 'str'>
  
result = dict((('a', 1) ,('b', 2), ('c', 3)))
print("Result: ", result)     # {'a': 1, 'b': 2, 'c': 3}
print("Type: ", type(result)) # <class 'dict'>
Navigation