Eval ()
Function: evaluates the string str as a valid expression and returns the result of the calculation.
Syntax: eval (source[, globals[, locals]), value
Parameters:
Source: The code object returned by a Python expression or function compile ()
Globals: Optional. It must be dictionary.
Locals: Optional. Arbitrary map Object
Examples show:
You can convert list,tuple,dict and string to each other. #################################################string converted to list >>>a = "[[+], [3,4], [5,6], [7,8], [9,0]]" >>>type (a) <type ' str ' >>> > b = eval (a) >>> print b[[1, 2], [3, 4], [5, 6], [7, 8], [9, 0]] >>> type (b) <type ' List ' > ################################################# string converted to dictionary >>> a = ' {1: ' A ', 2: ' B '} ' >> > type (a) <type ' str ' > >>> b = eval (a) >>> print b{1: ' A ', 2: ' B '}>>>< c11> type (b) <type ' dict ' > ################################################# string Conversion Narimoto Group >>> a = "([ [3,4], [5,6], [7,8], (9,0)) ">>> type (a) <type ' str ' >>>> b = eval (a) >>> Print B ([1, 2], [3, 4], [5, 6], [7, 8], (9, 0)) >>> type (b) <type ' tuple ' > /c14>
Isinstance ()
def isastring (obj): return isinstance (obj, str) if isastring ('1'): print(' is a string ')
python-built-in functions-strings, eval,isinstance