How Does Python Convert Characters To Numbers And Vice Versa

This article will introduce some python conversion functions that can convert between characters and numbers.

1. Conversion functions between characters and numbers in Python.

  1. chr(x): converts an integer to a character.
    >>> chr(99)
    'c'
  2. complex (real [, imag]): creates a complex number.
    >>> complex(1)
    (1+0j)
    >>> 
    >>> complex(1, 2)
    (1+2j)
    >>> 
    >>> complex('hello', 2)
    Traceback (most recent call last):
      File "<stdin>", line 1, in <module>
    TypeError: complex() can't take second arg if first is a string
    >>> 
    >>> 
    >>> complex('hello')
    Traceback (most recent call last):
      File "<stdin>", line 1, in <module>
    ValueError: complex() arg is a malformed string
    >>> 
    >>> 
    >>> complex('111')
    (111+0j)
    
  3. eval (str): evaluates a valid Python expression in a string and returns an object.
    >>> eval('1+2')
    3
    >>> eval('1+abc')
    Traceback (most recent call last):
      File "<stdin>", line 1, in <module>
      File "<string>", line 1, in <module>
    NameError: name 'abc' is not defined
    >>> 
    >>> 
    >>> abc = 1
    >>> 
    >>> eval('1+abc')
    2
    
  4. float (x): converts x to a floating-point number.
    >>> float(1)
    1.0
    >>> 
    >>> float(1+2)
    3.0
    >>> float('1+2')
    Traceback (most recent call last):
      File "<stdin>", line 1, in <module>
    ValueError: could not convert string to float: '1+2'
    >>> 
    >>> float('1')
    1.0
    
  5. hex(x): converts an integer to a hexadecimal string.
    >>> hex(10)
    '0xa'
    >>> 
    >>> 
    >>> hex('10')
    Traceback (most recent call last):
      File "<stdin>", line 1, in <module>
    TypeError: 'str' object cannot be interpreted as an integer
    >>> 
    >>> 
    >>> hex(10.00)
    Traceback (most recent call last):
      File "<stdin>", line 1, in <module>
    TypeError: 'float' object cannot be interpreted as an integer
  6. int (x [, base]): converts x to an integer.
    >>> int(a)
    Traceback (most recent call last):
      File "<stdin>", line 1, in <module>
    NameError: name 'a' is not defined
    >>> 
    >>> 
    >>> a = 1.2
    >>> 
    >>> int(a)
    1
    >>> 
    >>> int(2)
    2
    >>> int('2.5')
    Traceback (most recent call last):
      File "<stdin>", line 1, in <module>
    ValueError: invalid literal for int() with base 10: '2.5'
    >>> 
    >>> 
    >>> int(2.5)
    2
    
  7. list(s): converts the sequence s into a list.
    >>> list('hello python')
    ['h', 'e', 'l', 'l', 'o', ' ', 'p', 'y', 't', 'h', 'o', 'n']
    >>> 
    >>> 
    >>> list(1234567890)
    Traceback (most recent call last):
      File "<stdin>", line 1, in <module>
    TypeError: 'int' object is not iterable
    >>> 
    >>> list('1234567890')
    ['1', '2', '3', '4', '5', '6', '7', '8', '9', '0']
  8. long (x [, base]): converts x to a long integer.
    '''
    The reason for the below error is because there is no long type in the later version of Python 3.X, only int type exists in python3.x and later.
    There are both long and int types in Python 2.X. So just change long() to int()
    '''
    >>> long(1)
    Traceback (most recent call last):
      File "<stdin>", line 1, in <module>
    NameError: name 'long' is not defined
  9. oct(x): converts an integer to an octal string.
    >>> oct(1)
    '0o1'
    >>> 
    >>> 
    >>> oct('1')
    Traceback (most recent call last):
      File "<stdin>", line 1, in <module>
    TypeError: 'str' object cannot be interpreted as an integer
    >>> 
    >>> oct('1+2')
    Traceback (most recent call last):
      File "<stdin>", line 1, in <module>
    TypeError: 'str' object cannot be interpreted as an integer
    >>> 
    >>> oct(1+2)
    '0o3'
    
  10. ord(x): converts a character to its integer value.
    >>> ord('c')
    99
    >>> ord(1)
    Traceback (most recent call last):
      File "", line 1, in 
    TypeError: ord() expected string of length 1, but int found
    >>> 
    >>> ord('1+1')
    Traceback (most recent call last):
      File "", line 1, in 
    TypeError: ord() expected a character, but string of length 3 found
    
    
  11. repr(x): converts object x to an expression string.
    >>> repr(1)
    '1'
    >>> 
    >>> repr('I love python')
    "'I love python'"
    >>> 
    >>> repr({'python':'1', 'java':'2'})
    "{'python': '1', 'java': '2'}"
    >>> 
    
  12. str(x): converts object x to a string.
    >>> str(1)
    '1'
    >>> 
    >>> 
    >>> str(1+2)
    '3'
    >>> 
    >>> str('1+2')
    '1+2'
    >>> str('1+2.5')
    '1+2.5'
    >>> 
    >>> 
    >>> str(1+2.5)
    '3.5'
    
  13. tuple(s): converts the sequence s into a tuple.
    >>> tuple('I love python')
    ('I', ' ', 'l', 'o', 'v', 'e', ' ', 'p', 'y', 't', 'h', 'o', 'n')
    >>> 
    >>> tuple('')
    ()
    >>> 
    >>> tuple(123)
    Traceback (most recent call last):
      File "<stdin>", line 1, in <module>
    TypeError: 'int' object is not iterable

Leave a Comment

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.