How to Run Python Code in ChatGPT

5/5 - (2 votes)

To run Python code in ChatGPT, simply copy and paste or type the code in the prompt input field. ChatGPT figures out code automatically and internally calls the code interpreter.

If you want newlines, you need to hit SHIFT + ENTER instead of ENTER only to prevent the partial code from being submitted prematurely.

For example, you can paste these two lines in the input field:

And the output is:

In this case, it wrongly saw it as Golang code. If you want explicit Python code, try natural language:

Run the following Python code:
for i in range(10):
    print('hello world')

By the way, you can also pass a screenshot of your Python code like so:

ChatGPT correctly identifies the code (OCR) and runs it internally:

If you want to keep learning, check out this article:

πŸ‘‰ Chat(GPT) with Images – These Five Prompts Will Impress Your Coworkers