Scripting >> Python >> Examples >> Functions >> Data Type Conversion Functions

 

 

Function    
int(x [,base]) Converts x to an integer. The base specifies the base if x is a string. >>> myInteger=int('11111111',2)
>>> print myInteger
255
float(x) Converts x to a floating-point number. >>> float(1)
1.0
>>> float('1')
1.0
complex(real [,imag]) Creates a complex number. >>> complex(1,-1)
(1-1j)
>>> complex(1,+1)
(1+1j)
>>> complex(1,1)
(1+1j)
str(x) Converts object x to a string representation. >>> print("Integer = " + str(1))
Integer = 1
repr(x) Converts object x to an expression string.  
eval(str) Evaluates a string and returns an object.  
tuple(s) Converts s to a tuple.  
list(s) Converts s to a list.  
set(s) Converts s to a set.  
dict(d) Creates a dictionary. d must be a sequence of (key,value) tuples.  
frozenset(s) Converts s to a frozen set.  
chr(x) Converts an integer to a character  
unichr(x) Converts an integer to a Unicode character.  
ord(x) Converts a single character to its integer value.  
hex(x) Converts an integer to a hexadecimal string.  
oct(x) Converts an integer to an octal string.