+

Type conversion in Python

1. int (a, base) : This function will convert any data type to an integer . Base indicates the base the string is in if the data type is string.

2. float () : This function is used to convert any data type to a floating point number. 
,

Output:

 After converting to integer base 2: 18 After converting to float: 10010.0 

3 ... ord (): this function is used to convert a character to an integer.

4. hex (): this function is for converting a integer to a hexadecimal string .

5. oct (): this function converts a integer to an octal string .

# Python code to demonstrate type conversion
# using int (), float ()

 
# initializing string

s = "10010"

 
# print a string converting to int base 2

c = int (s, 2 )

print ( "After converting to integer base 2:" , end = < code class = "plain"> "")

print (c)

 
# print string converted to float

e = float (s)

print ( "After converting to float: " , end = " ")

print (e)

# Python code to demonstrate type conversion
# using ord (), hex (), oct ()

 
# integer initialization

s = `4`

  
# print characters with integer conversion

c = ord (s)

print ( "After converting character to integer:" , end < code class = "keyword"> = "")

print (c)

 
# output integer to hexadecimal string

c = hex ( 56 )

print ( "After converting 56 to hexadecimal string:" , end = " ")

print (c)

 
# output an integer to an octal string

c = oct ( 56 )

print ( "After converting 56 to octal string:" , end = "")

print (c)

Exit :

 After converting character to integer: 52 After converting 56 to hexadecimal string: 0x38 After converting 56 to octal string: 0o70 

6. tuple (): this function is used to convert to tuple .

7. set (): this function returns the type after conversion to set .

8. list (): this function is used to convert any data type to a list type .

# Python code for type conversion demonstrations
# using tuple (), set (), list ()

 
# initialization string

s = ` geeks`

 
# output the string to convert to a tuple

c = tuple (s)

print ( "After converting string to tuple:"  , end = "")

print (c)

 
# output string to convert to set

c = set (s)

print ( "After converting string to set:" , end = "")

print (c)

 
# print a string that converts to a 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`] 

9. dict (): this function is used to convert an order tuple (key, value) into a dictionary .

10. str (): is used to convert an integer to a string.

11. complex (real, imag) :: this function converts real numbers to complex (real, imag) numbers.

# Python code to demonstrate type conversion
# using dict (), complex (), str ()

 
# initializing integers

a = 1

b = 2

 
# tuple initialization

tup = (( ` a` , 1  ), ( `f` , 2 ), ( `g` , 3 ))

 
# output an integer to a complex number

c = complex ( 1 , 2 )

print ( "After converting integer to complex number:" , end = "")

print (c)

 
# output an integer to a string

c = str (a)

print ( " After converting integer to string: " , end = "")

print (c)

 
# printing tuple-to-expression dictionary conversion

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} 

This article courtesy of Manjit Singh (S. Nandini) . If you are as Python.Engineering and would like to contribute, you can also write an article using contribute.python.engineering or by posting an article contribute @ python.engineering. See my article appearing on the Python.Engineering homepage and help other geeks.

Please post comments if you find anything wrong or if you would like to share more information on the topic discussed above.

Get Solution for free from DataCamp guru