Type conversion functions

Revision as of 20:10, 19 April 2011 by Smitschu (talk | contribs)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)

Type conversion functions are used in programming to convert data to primitive datatypes.

Type Conversion in Python

  • str(x) returns a string version of x. Almost all datatypes support this function, and you can use it on your own classes by implementing the __str__ function.
  • int(x) converts x into an integer. Floats will be rounded down to the nearest integer, and strings that don't represent a valid integer will result in an error.
  • float(x) converts x into a floating point number. Strings that don't represent a valid floating point number will result in an error.

Sequence Conversion