When it is required to convert the location co-ordinates into a tuple format, the 'eval' method can be used.
The 'eval' method parses the expression which is passed to it as an argument. It executes that arguments as the code. It returns the result which is evaluated from the 'expression', i.e the parameter.
Below is a demonstration of the same −
Example
my_string = "67.5378, -78.8523" print("The string is : ") print(my_string) my_result = eval(my_string) print("The coordinates after converting the string to tuple is : ") print(my_result)
Output
The string is : 67.5378, -78.8523 The coordinates after converting the string to tuple is : (67.5378, -78.8523)
Explanation
- A string is defined, and is displayed on the console.
- The 'eval' method is used, and the string is passed as parameter to it.
- This is assigned to a value.
- It is displayed on the console.