Sometimes, you may need to perform conversions between the built-in types. To convert between types, you simply use the type name as a function.
There are several built-in functions to perform conversion from one data type to another. These functions return a new object representing the converted value.
Sr.No. | Function & Description |
---|---|
1 | int(x [,base]) Converts x to an integer. base specifies the base if x is a string. |
2 | long(x [,base] ) Converts x to a long integer. base specifies the base if x is a string. |
3 | float(x) Converts x to a floating-point number. |
4 | complex(real [,imag]) Creates a complex number. |
5 | str(x) Converts object x to a string representation. |
6 | repr(x) Converts object x to an expression string. |
7 | eval(str) Evaluates a string and returns an object. |
8 | tuple(s) Converts s to a tuple. |
9 | list(s) Converts s to a list. |
10 | set(s) Converts s to a set. |
11 | dict(d) Creates a dictionary. d must be a sequence of (key,value) tuples. |
12 | frozenset(s) Converts s to a frozen set. |
13 | chr(x) Converts an integer to a character. |
14 | unichr(x) Converts an integer to a Unicode character. |
15 | ord(x) Converts a single character to its integer value. |
16 | hex(x) Converts an integer to a hexadecimal string. |
17 | oct(x) Converts an integer to an octal string. |