Explicit Type Conversion in Python
In Explicit Type Conversion in Python, the data type is manually changed by the user as per their requirement. With explicit type conversion, there is a risk of data loss since we are forcing an expression to be changed in some specific data type. Various forms of explicit type conversion are explained below:
Converting integer to float
int(a, base): This function converts any data type to an integer. ‘Base’ specifies the base in which the string is if the data type is a string.
float(): This function is used to convert any data type to a floating-point number.
Python3
# initializing string s = "10010" # printing string converting to int base 2 c = int (s, 2 ) print ( "After converting to integer base 2 : " , end = "") print (c) # printing string converting to float e = float (s) print ( "After converting to float : " , end = "") print (e) |
Output:
After converting to integer base 2 : 18
After converting to float : 10010.0
Python Type conversion using ord(), hex(), oct()
ord(): This function is used to convert a character to an integer.
hex(): This function is to convert an integer to a hexadecimal string.
oct(): This function is to convert an integer to an octal string.
Python3
# initializing integer s = '4' # printing character converting to integer c = ord (s) print ( "After converting character to integer : " ,end = "") print (c) # printing integer converting to hexadecimal string c = hex ( 56 ) print ( "After converting 56 to hexadecimal string : " ,end = "") print (c) # printing integer converting to octal string c = oct ( 56 ) print ( "After converting 56 to octal string : " ,end = "") print (c) |
Output:
After converting character to integer : 52
After converting 56 to hexadecimal string : 0x38
After converting 56 to octal string : 0o70
Python Type conversion using tuple(), set(), list()
tuple(): This function is used to convert to a tuple.
set(): This function returns the type after converting to set.
list(): This function is used to convert any data type to a list type.
Python3
# initializing string s = 'geeks' # printing string converting to tuple c = tuple (s) print ( "After converting string to tuple : " ,end = "") print (c) # printing string converting to set c = set (s) print ( "After converting string to set : " ,end = "") print (c) # printing string converting to list c = list (s) print ( "After converting string to list : " ,end = "") print (c) |
Output:
After converting string to tuple : ('g', 'e', 'e', 'k', 's')
After converting string to set : {'k', 'e', 's', 'g'}
After converting string to list : ['g', 'e', 'e', 'k', 's']
Python code to demonstrate Type conversion using dict(), complex(), str()
dict(): This function is used to convert a tuple of order (key, value) into a dictionary.
str(): Used to convert an integer into a string.
complex(real,imag) : This function converts real numbers to complex(real,imag) number.
Python3
# initializing integers a = 1 b = 2 # initializing tuple tup = (( 'a' , 1 ) ,( 'f' , 2 ), ( 'g' , 3 )) # printing integer converting to complex number c = complex ( 1 , 2 ) print ( "After converting integer to complex number : " ,end = "") print (c) # printing integer converting to string c = str (a) print ( "After converting integer to string : " ,end = "") print (c) # printing tuple converting to expression dictionary c = dict (tup) print ( "After converting tuple to dictionary : " ,end = "") print (c) |
Output:
After converting integer to complex number : (1+2j)
After converting integer to string : 1
After converting tuple to dictionary : {'a': 1, 'f': 2, 'g': 3}
Convert ASCII value to characters
chr(number): This function converts a number to its corresponding ASCII character.
Python3
a = chr ( 76 ) b = chr ( 77 ) print (a) print (b) |
Output:
L
M
Type Conversion in Python
Python defines type conversion functions to directly convert one data type to another which is useful in day-to-day and competitive programming. This article is aimed at providing information about certain conversion functions.
There are two types of Type Conversion in Python:
- Python Implicit Type Conversion
- Python Explicit Type Conversion
Contact Us