5 – Type Conversions

It is very important to convert from one data type to another, because many inbuilt modules restrict to certain data types. Some of the type conversions are listed below.

  1. Binary String to Integer with base 2

  2. 1
    2
    3
    4
    5
    6
    7
    8
    9
    
    >>>b='001101101'
    >>> int(b,2)
    109
    >>>
    >>> b='001010011'
    >>> b
    '001010011'
    >>> int(b,2)
    83
  3. Integer to Binary String

  4. 1
    2
    3
    4
    5
    6
    7
    8
    9
    
    >>> a=45
    >>> a
    45
    >>> bin(a)
    '0b101101'
    >>>
    >>> #To eliminate the '0b'
    >>> bin(a)[2:]
    '101101'
  5. Hex String to Integer with base 16

  6. 1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    
    hs='FF'
    >>> hs
    'FF'
    >>> int(hs,16)
    255
    >>>
    >>> hs='45'
    >>> hs
    '45'
    >>> int(hs,16)
    69
  7. Integer to Hex String

  8. 1
    2
    3
    4
    5
    6
    7
    8
    
    >>> a=886
    >>> a
    886
    >>> hex(a)
    '0x376'
    >>> #To elimate '0x'
    >>> hex(a)[2:]
    '376'
  9. Hex String to Hex

  10. 1
    2
    3
    4
    5
    6
    7
    
    >>> a=6464
    >>> h=hex(a)
    >>> h
    '0x1940'
    >>> int(h[2:])
    1940
    >>>
  11. Character to Integer

  12. 1
    2
    3
    4
    5
    6
    7
    
    >>> c='f'
    >>> c
    'f'
    >>> ord(c)
    102
    >>> bin(ord(c))[2:]
    '1100110'
  13. String to binary list

  14. 1
    2
    3
    4
    5
    6
    
    data="Hello"
    >>> for ch in data:
    	bind.append(bin(ord(ch))[2:].zfill(8))
    >>>	
    >>> bind
    ['00001010', '01001000', '01100101', '01101100', '01101100', '01101111']
  15. String or Integer to Float

  16. 1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    
    s='4646.14'
    >>> s
    '4646.14'
    >>> float(s)
    4646.14
    >>>
    >>> i=466
    >>> i
    466
    >>> float(i)
    466.0
    >>>
  17. Restrict the no of digits in float

  18. 1
    2
    3
    4
    5
    6
    
     x=343.34343
    >>> format(x, '.2f')
    '343.34'
    >>> r=format(x, '.2f')
    >>> float(r)
    343.34
  19. Any type to String

  20. 1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    
    >>> i=366
    >>> str(i)
    '366'
    >>>
    >>> i=16.11
    >>> str(i)
    '16.11'
    >>>
    >>> i=[2,24,4]
    >>> for d in range(len(i)):
    	i[d]=str(i[d])
    >>> i
    ['2', '24', '2']
    >>>
  21. String to tuple and List

  22. 1
    2
    3
    4
    5
    6
    7
    
    s='MYBTECHPROJECTS'
    >>> tuple(s)
    ('M', 'Y', 'B', 'T', 'E', 'C', 'H', 'P', 'R', 'O', 'J', 'E', 'C', 'T', 'S')
    >>>
    >>> list(s)
    ['M', 'Y', 'B', 'T', 'E', 'C', 'H', 'P', 'R', 'O', 'J', 'E', 'C', 'T', 'S']
    >>>
  23. List or Tuple to a String

  24. 1
    2
    3
    4
    5
    6
    7
    8
    9
    
    t=('M', 'Y', 'B', 'T', 'E', 'C', 'H', 'P', 'R', 'O', 'J', 'E', 'C', 'T', 'S')
    >>>
    >>> ''.join(t)
    'MYBTECHPROJECTS'
    >>>
    >>> l=['M', 'Y', 'B', 'T', 'E', 'C', 'H', 'P', 'R', 'O', 'J', 'E', 'C', 'T', 'S']
    >>>
    >>> ''.join(l)
    'MYBTECHPROJECTS'
  25. Tuple to dict

  26. 1
    2
    3
    4
    5
    6
    7
    8
    
    t=(('one',1),('two',2),('three',3))
    >>> d=dict(t)
    >>> d['one']
    1
    >>> d['two']
    2
    >>> d['three']
    3
THANK YOU
SHARE THIS!!!

Gowtham S

Gowtham is a programming enthusiast. His field of interest includes Arduino, NodeMCU, Raspberry Pi, and Python. To know more about him visit https://mybtechprojects.tech/about-us/.

3 Comments

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.