← Go Back

How to Execute Python Code Contained in a String

Using the exec() built-in function, which takes a string that must contain Python code.

>>> code = "print('Hello, world.')"
>>> exec(code)
Hello, world.

This function has no return value.

strings compilation


🐍 You might also find interesting: