# Type Conversion in Python

PythonProgrammingServer Side Programming

Using Python, we can easily convert data into different types. There are different functions for Type Conversion. We can convert string type objects to numeric values, perform conversion between different container types etc.

In this section we will see how the conversions can be done using Python.

## Converting String to Numeric Types

To convert from String type objects to Numeric Objects, there are different methods like int(), float() etc. Using the int() method we can convert any number as string to integer value (base 10). It takes the string type argument, default base is 10, We can also specify the base to convert from a string of that base to decimal number.

Similarly using the float() method, one string which contains value in decimal form, can be converted as float.

## Example code

Live Demo

str_number = '56'
print(int(str_number))    #default base is 10
print(int(str_number, 12))    #From a number where base is 12
str_number = '25.897'
print(float(str_number))    #convert string to floating point value


## Output

56
86
66
25.897


## String to character converting and Base converting

As we know the strings are the collection of characters. But in Python, we cannot directly get the ASCII value of the characters. We need to use the ord() method to convert character to its ASCII value.

There are another some methods like hex(), ord(), bin() to convert decimal number to Hexadecimal, Octal, Binary numbers respectively.

## Example code

Live Demo

print('ASCII value of "G" is: ' + str(ord('G')))
print('Hexadecimal value of 254 is: ' + str(hex(254)))
print('Octal value of 62 is: ' + str(oct(62)))
print('Binary value of 56 is: ' + str(bin(56)))


## Output

ASCII value of "G" is: 71
Hexadecimal value of 254 is: 0xfe
Octal value of 62 is: 0o76
Binary value of 56 is: 0b111000


## Converting Containers

In Python, there are different container type objects like list, tuples, sets etc. We can change one type of containers to another type of containers using list(), tuple(), set() etc.

## Example code

Live Demo

my_list = [10, 20, 30, 40, 50]
my_set = {10, 10, 20, 30, 20, 50, 20}
print('From list to tuple: ' + str(tuple(my_list)))
print('From list to set: ' + str(set(my_list)))
print('From set to list: ' + str(list(my_set)))


## Output

From list to tuple: (10, 20, 30, 40, 50)
From list to set: {40, 10, 50, 20, 30}
From set to list: [10, 20, 50, 30]


## Complex Numbers

In Python there is Complex number class. So using this, we can convert two integers (real and imaginary part) to complex numbers.

## Example code

Live Demo

my_complex = complex(10, 5) #convert to complex number
print(my_complex)


## Output

(10+5j)


## Tuple to Dictionaries

Tuple is one of the most important container in Python. Using tuples, we can store some ordered data. In Python, we can convert Tuple type objects with two values to dictionary objects. The dict() method can do the conversion.

## Example code

Live Demo

my_tuples = (('Tiger', 4), ('Cat', 6), ('Dog', 8), ('Elephant', 10))
my_dict = dict(my_tuples)
print(my_dict)


## Output

{'Tiger': 4, 'Elephant': 10, 'Dog': 8, 'Cat': 6}

Published on 14-Nov-2018 12:51:01