How do we evaluate a string and return an object in Python?


By using the eval() function in python we can evaluate a string and return a python object. The eval() is a python built−In function that evaluates a string argument by parsing the string as a code expression.

Syntax

eval(expression[, globals[, locals]])

Parameters

  • expression: It’s a string that will be evaluated as a Python expression.

  • globals: An optional parameter, which is a dictionary containing global parameters.

  • locals: An optional parameter, which is a dictionary containing local parameters.

  • Return: Returns the result evaluated from the expression.

If the string containing arithmetic expression

If we pass a string containing arithmetic expression to the eval() function, first it parses expression then evaluates it, and finally returns an evaluated python object.

Example

var = 100
string_EXP_1 = "2*4"
string_EXP_2 = "var + 20"

print("string expression 1: ", string_EXP_1)
print("string expression 2: ", string_EXP_2)

# use the eval method 
result_1 = eval(string_EXP_1)
result_2 = eval(string_EXP_2)

print("Output for Expression 1: ", result_1)
print("Output for Expression 1: ", result_2)

Output

string expression 1:  2*4
string expression 2:  var + 20
Output for Expression 1:  8
Output for Expression 1:  120

As we see in the above example the eval() successfully evaluated the string formed arithmetic expression and returned an integer object. Also we can see that function accessed a global variable x.

If the string containing list/tuple

The eval() function can also evaluate a string which contains a list/tuple. That means it returns a list/tuple object based on string data.

Example

string = "1, 2"

print("Input String: ", string)
print('type', type(string))

# apply the eval method 
result = eval(string)

print("Output : ", result)
print("Output object type: ", type(result))

Output

Input String: 1, 2
type <class 'str'>
Output :  (1, 2)
Output object type:  <class 'tuple'>

The input string contains two elements separated by comma, in python the comma separated values are treated as elements of tuple. So that the above example returned a tuple object after applying the eval() function.

Example

Like the previous example the eval() function evaluated string and returned a list object.

string = "[45, 9, 16]"

print("Input String: ", string)
print('type', type(string))

# apply the eval method 
result = eval(string)

print("Output : ", result)
print("Output object type: ", type(result))

Output

Input String:  [45, 9, 16]
type <class 'str'>
Output :  [45, 9, 16]
Output object type:  <class 'list'>

If the string containing list comprehension expression

The eval() function can also evaluate a list comprehension expression and returns a python list object, if the input string contains list comprehension expression.

Example

The eval function successfully evaluated the list comprehension expression and returned a python list object.

string = '[a*2 for a in range(5)]'

print("Input String: ", string)
print('type', type(string))

# apply the eval method 
result = eval(string)

print("Output : ", result)
print("Output object type: ", type(result))

Output

Input String:  [a*2 for a in range(5)]
type <class 'str'>
Output :  [0, 2, 4, 6, 8]
Output object type:  <class 'list'>

If the string containing a python dictionary

If the string contains a python dictionary, then the eval() method returns it an original Python dictionary object.

Example

String_dict = "{'One': 1, 'two': 2, 'three': 3}"
print("String represented dictionary: ",String_dict)
print(type(String_dict))

# use the eval method 
dict_ = eval(String_dict)
print("Output: ", dict_)
print(type(dict_))

Output

String represented dictionary:  {'One': 1, 'two': 2, 'three': 3}
<class 'str'>
Output:  {'One': 1, 'two': 2, 'three': 3}
<class 'dict'>

As we see in the above output block The eval() function returns a python dictionary object.

Updated on: 23-Aug-2023

599 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements