In this Python Tutorial, we will learn how to convert values from one data type to another data type using builtin function like int(x), float(x), complex(x), str(x), tuple(x), set(x), frozenset(x), list(x), dict(x), and chr(x).
Python – Variable Data Type Conversion
Typically, there could be scenarios where you may need to convert a value of a data type to another data type.
Python provides following built-in functions to convert values to a specific datatype:
- Convert to Integer or Long
- Convert to Float
- Convert to Complex
- Convert to Character
- Convert to String
- Convert to Tuple
- Convert to List
- Convert to Set
- Convert to Frozen Set
- Convert to Dictionary
Convert to Integer or Long
int(x) is used to convert x to int
int(x,base) is used if x is a string, and base specifies thebase of the value in x to be converted to integer. If base is not specified when x is a string, 10 is taken as default base.
Following program demonstrates the conversion of float, string and complex numbers to int.
example.py – Python Program
float_value = 5.2 str_value = "34" complex_value = 3.14j float_value_to_int = int(float_value) str_value_to_int = int(str_value) str_value_to_int_base_8 = int(str_value,8) str_value_to_int_base_6 = int(str_value,6) complex_value_to_int = int(abs(complex_value)) print(float_value_to_int) print(str_value_to_int) print(str_value_to_int_base_8) print(str_value_to_int_base_6) print(complex_value_to_int)Try Online
Output
5 34 28 22 3
* Note : By default, if the value could not be accommodated in an integer, the data type is automatically upgraded to long by the python interpreter. If the value is so long that it could not fit in an integer, then we got python interpreter to upgrade it to long without our attention required.
Convert to Float
float(x) is used to convert x to float.
Following program demonstrates the conversion of int, string and complex numbers to float.
example.py – Python Program
int_value = 254 str_value = "34.68" complex_value = 3.14j long_value_to_float = float(int_value) str_value_to_float = float(str_value) complex_value_to_float = float(abs(complex_value)) print(long_value_to_float) print(str_value_to_float) print(complex_value_to_float)Try Online
Output
254.0 34.68 3.14
Convert to Complex
complex(real) is used to convert x to complex value with no imaginary value as 0.
complex(real, imag) is used to convert x to complex value.
example.py – Python Program
real_value = 254 imaginary_value = 394 to_complex_only_real = complex(real_value) to_complex = complex(real_value, imaginary_value) print(to_complex_only_real) print(to_complex)Try Online
Output
(254+0j) (254+394j)
Convert to Character
chr(x) converts the integer x to character.
example.py – Python Program
int_value_1 = 69 int_value_2 = 81 int_to_character_1 = chr(int_value_1) int_to_character_2 = chr(int_value_2) print(int_to_character_1) print(int_to_character_2)Try Online
Output
E Q
Conversion to String
str(x) is used to convert x to string
Following program demonstrates the conversion of float, int, complex number, tuple, list and dictionary to string.
example.py – Python Program
float_value = 56.369 long_value = 6369742212323693323265 complex_value = (214 + 254j) tuple_value = ("John","spy",42) list_value = [1,'India',36.45,"New Delhi"] dict_value = {'country':'India','capital':'New Delhi'} float_value_to_string = str(float_value) long_value_to_string = str(long_value) complex_value_to_string = str(complex_value) tuple_value_to_string = str(tuple_value) list_value_to_string = str(list_value) dict_value_to_string = str(dict_value) print(float_value_to_string) print(long_value_to_string) print(complex_value_to_string) print(tuple_value_to_string) print(list_value_to_string) print(dict_value_to_string) print() print(float_value_to_string + long_value_to_string + complex_value_to_string + tuple_value_to_string + list_value_to_string + dict_value_to_string)Try Online
Output
56.369 6369742212323693323265 (214+254j) ('spy', 42, 'John') [1, 'India', 36.45, 'New Delhi'] {'country': 'India', 'capital': 'New Delhi'} 56.3696369742212323693323265(214+254j){'spy', 42, 'John'}[1, 'India', 36.45, 'New Delhi']{'country': 'India', 'capital': 'New Delhi'}
Convert to Tuple
tuple(x) converts x to tuple.
example.py – Python Program
str_value = "hello" str_to_tuple = tuple(str_value) print(str_to_tuple) print(str_to_tuple[0]) print(str_to_tuple[1]) print(str_to_tuple[2]) print(str_to_tuple[3]) print(str_to_tuple[4])Try Online
Output
('h', 'e', 'l', 'l', 'o') h e l l o
The string is made up of characters and when the string is converted to tuple, the characters would become elements of the tuple.
Convert to List
list(x) converts x to tuple with elements that are “iteratable over” in x. If x is a string, string could be iterated over its characters and characters would become the elements of the newly formed list.
example.py – Python Program
str_value = "hello" str_to_list = list(str_value) print(str_to_list)Try Online
Output :
['h', 'e', 'l', 'l', 'o']
Convert to Set
set(x) converts x to tuple with elements that are “iteratable over” in x. If x is a string, string could be iterated over its characters and characters would become the elements of the newly formed set. And only unique elements are allowed in a set.
example.py – Python Program
str_value = "hello Nemo!" str_to_list = set(str_value) print(str_to_set)Try Online
Output
{'h', 'o', 'l', ' ', '!', 'm', 'N', 'e'}
Convert to Frozen Set
frozenset(x) converts x to tuple with elements that are “iteratable over” in x. If x is a string, string could be iterated over its characters and characters would become the elements of the newly formed list.
example.py – Python Program
str_value = "hello Nemo!" str_to_frozenset = frozenset(str_value) print(str_to_frozenset)Try Online
Output
frozenset({' ', 'm', 'N', 'h', 'l', 'e', '!', 'o'})
Convert to Dictionary
dict(x) converts x, sequence or list of tuples (key, value), into dictionary.
Python Variable Data Type Conversion from list of tuples to dictionary
example.py – Python Program
tupils_list = z = [('1','a'), ('2','b'), ('3','c')] str_to_dict = dict(tupils_list) print(str_to_dict)Try Online
Output
{'1': 'a', '2': 'b', '3': 'c'}
Conclusion
In this Python Tutorial, we have learnt Python Variable Data Type Conversion.