Type Conversion
Python defines type conversion functions to directly convert one data type to another which is useful in day to day and competitive programming.
Example 1 :
#int(a,base) : This function converts any data type to integer.
#‘Base’ specifies the base in which string is if data type is string.
# initializing string
s = "10010"
# printing string converting to int base 2
c = int(s,2)
print ("After converting to integer base 2 : ", c) After converting to integer base 2 : 18
Example 2:
After converting to float : 10010.0
Example 3:
After converting string to tuple : ('p', 'y', 't', 'h', 'o', 'n')
After converting string to set : {'p', 'n', 'y', 'o', 't', 'h'}
After converting string to list : ['p', 'y', 't', 'h', 'o', 'n']
Example 4:
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}
Last updated
Was this helpful?