How to call dynamicly string operations in Python?

Question:

//func_to_exec parameter is coming from database dynamically.
func_to_exec='split("|")[0].split(",")[1]'

pl='mancity,manunited,arsenal|2|3|4|5'

is there anyway to call

pl.func_to_exec

I saw exec and eval functions are only for integers. I cant find any solution for strings.
Thx for suggestions.

Asked By: nebuchadnezzar

||

Answers:

You can use the exec function for that:

pl = 'mancity,manunited,arsenal|2|3|4|5'
func_to_exec = 'split("|")[0].split(",")[1]'

exec(f'result = pl.{func_to_exec}')
print(result)  # Output: 'manunited'
Answered By: kalzso
Categories: questions Tags:
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.