+ 1

How can I convert string to keyword?

https://code.sololearn.com/crjHLZDky29i/?ref=app The string doesn't get recognized as the assigned keyword and I can't do string calculations otherwise.

13th May 2019, 10:22 AM
Akko Rishtar
3 Answers
+ 2
print(eval("bobobo")+eval("bobobo")) With eval you can interpret strings as expressions. There's probably a better way. Wanna describe what exactly you're planning to do?
13th May 2019, 10:37 AM
HonFu
HonFu - avatar
+ 2
Thank you. I have no plans for this, I'm learning my first language and trying to see in which ways I can or cannot use different functions, so I can find solutions more easily in the future, if need arises.
13th May 2019, 10:43 AM
Akko Rishtar
+ 1
I might add that you can only use eval for strings that have a value. If you want to parse whole code, you'll need exec instead of eval. So for example: exec('while True: print("Hello!")')
13th May 2019, 10:49 AM
HonFu
HonFu - avatar