- Python Basic Tutorial
- Python - Home
- Python - Overview
- Python - Environment Setup
- Python - Basic Syntax
- Python - Comments
- Python - Variables
- Python - Data Types
- Python - Operators
- Python - Decision Making
- Python - Loops
- Python - Numbers
- Python - Strings
- Python - Lists
- Python - Tuples
- Python - Dictionary
- Python - Date & Time
- Python - Functions
- Python - Modules
- Python - Files I/O
- Python - Exceptions
- Python Advanced Tutorial
- Python - Classes/Objects
- Python - Reg Expressions
- Python - CGI Programming
- Python - Database Access
- Python - Networking
- Python - Sending Email
- Python - Multithreading
- Python - XML Processing
- Python - GUI Programming
- Python - Further Extensions
How do I execute a string containing Python code in Python?
In this article, we are going to find out how to execute a string containing Python code in Python.
To execute a string containing Python code we should take the input string as multi-line input using triple quotes, then we will use the inbuilt function exec(). This will take a string as input and returns the output of the code that is present inside the string.
The exec() function is used to execute Python programmes dynamically. These programmes can be either strings or object code. If it's a string, it's translated into a series of Python statements that are then performed, barring any syntax errors; if it's object code, it's just executed.
We must be careful not to utilise return statements anywhere other than within the declaration of a function, not even in the context of code that is passed to the exec() method.
In the program given below, we are taking a multi-line coded string as input and we are finding out the output of that using the exec() method −
str1 = """ a = 3 b = 6 res = a + b print(res) """ print("The output of the code present in the string is ") print(exec(str1))
The output of the above example is as shown below −
The output of the code present in the string is 9 None
Using eval() function
To execute an expression that is present inside a string, we will use the inbuilt function eval() and pass the string to the function and the output of the code present inside the string is returned.
In the example given below, we are taking an expression as a string as an input and we are evaluating it using eval() method −
str1 = "3 + 5" print("The output of the code present in the string is ") print(eval(str1))
The output of the above example is given below −
The output of the code present in the string is 8
- Related Articles
- Execute a String of Code in Python
- How do I do a case insensitive string comparison in Python?
- How do I modify a string in place in Python?
- How do I wrap a string in a file in Python?
- How do I convert a string to a number in Python?
- How do I convert a number to a string in Python?
- How do I un-escape a backslash-escaped string in Python?
- How do I Input a String From the User in Python?
- How do I format a string using a dictionary in Python 3?
- How do you code a vending machine in Python?
- How to execute a Python file in Python shell?
- How do I check if a string has alphabets or numbers in Python?
- How do I remove a substring from the end of a string in Python?
- How do I delete a file in Python?
- How do I copy a file in python?