- Related Questions & Answers
- Data Type Conversion in Python
- Type Conversion in C++
- What is Type conversion in C#?
- What is type conversion in java?
- Catch block and type conversion in C++
- What is the difference between type conversion and type casting in C#?
- How to implement integer type conversion in JShell in Java 9?
- Decimal to binary list conversion in Python
- What is the difference between implicit and explicit type conversion in C#?
- What is the importance of '+' operator in type conversion in JavaScript?
- What are different data conversion methods in Python?
- Dictionary to list of tuple conversion in Python
- List of tuples to dictionary conversion in Python
- What is the type conversion operator ( ) in Java and how to use it?
- Type and isinstance in Python

- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who

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.

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.

str_number = '56' print(int(str_number)) #default base is 10 print(int(str_number, 16)) #From Hexadecimal 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

56 86 66 25.897

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.

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

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

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.

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

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]

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

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

(10+5j)

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.

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

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

Advertisements