Type Conversion

The following functions convert between types

     int(x)             Convert x to an integer
     long(x)            Convert x to a long integer
     float(x)           Convert x to a float
     complex(r,i)       Creates a complex number
     str(x)             Create a string
     repr(x)            Create an expression string
     eval(str)          Evaluate a string and return an object
     tuple(s)           Convert sequence s to a tuple
     list(s)            Convert sequence s to a list
     chr(x)             Convert an integer to a character
     ord(x)             Convert single characte to an integer code
     hex(x)             Convert x to a hex string
     oct(x)             Convert x to an octal string

str vs. repr

  • The str() function creates a string suitable for printing
  • The repr() function creates a string that can be evaluated to produce the original object
    
    
<<< O'Reilly OSCON 2000, Introduction to Python, Slide 60
July 17, 2000, beazley@cs.uchicago.edu
>>>