
- Python Basic Tutorial
- Python - Home
- Python - Overview
- Python - Environment Setup
- Python - Basic Syntax
- Python - Comments
- Python - Variables
- Python - Data Types
- Python - Operators
- Python - Decision Making
- Python - Loops
- Python - Numbers
- Python - Strings
- Python - Lists
- Python - Tuples
- Python - Dictionary
- Python - Date & Time
- Python - Functions
- Python - Modules
- Python - Files I/O
- Python - Exceptions
Type Conversion in Python
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
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
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
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
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
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
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}
- Related Articles
- Data Type Conversion in Python
- Type Conversion in C++
- What is Type Conversion?
- What is Type conversion in C#?
- What is type conversion in java?
- Difference Between Type casting and Type Conversion
- MS SQL Server - Type Conversion
- 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
