-3

I have a string 'user = input("Hello World")'. How can I execute the string to be able to run as a python command?

I am a new to python so I must have missed the basic syntax. please help me instead of routing me to another answer

  • 1
    Does this answer your question? [How do I execute a string containing Python code in Python?](https://stackoverflow.com/questions/701802/how-do-i-execute-a-string-containing-python-code-in-python) – Tom Myddeltyn Dec 08 '20 at 05:37
  • Please repeat [on topic](https://stackoverflow.com/help/on-topic) and [how to ask](https://stackoverflow.com/help/how-to-ask) from the [intro tour](https://stackoverflow.com/tour). – Prune Dec 08 '20 at 05:42
  • See [How much research](https://meta.stackoverflow.com/questions/261592/how-much-research-effort-is-expected-of-stack-overflow-users) and the [Question Checklist](https://meta.stackoverflow.com/questions/260648/stack-overflow-question-checklist) – Prune Dec 08 '20 at 05:42

1 Answers1

1

You can use either of two exec or eval to run the string as python command based on your desired syntax or result

eval('user = input("Hello World")')

exec('user = input("Hello World")')

Visit exec and eval to learn more about them

Aliasgher Nooruddin
  • 534
  • 1
  • 6
  • 18