How can I create a multi line input in ipython?

Viewed 13238

I am reading the book Python Data Science Handbook by Jake VanderPlas. In the first chapter a multi line input in ipython is illustrated:

multiLineInput

Can anybody please tell me, how to do that? I know how to write a block before executing it in Jupyter Notebook, but in the ipython shell I don't no how to do it. Thanks for your help!

6 Answers

ctrl+o

If you type ctrl+o, you should be able to add additional lines to the input to run them simultaneously.

If that doesn't work, as a workaround, you can paste multiple lines after copying them from elsewhere, or you can press enter on a line with a semicolon or unclosed parentheses. IPython will automatically give you another line to finish your statement rather than running and instantly hitting a syntax error.

Depending on the environment where you are running iPython you might also try ctrl+Enter or shift+Enter.

Use 'shift' + 'Enter' at the end of line to avoid execution and keep typing your code. When you are done writing your block of code hit 'Enter' Have a look to the following picture If for some reason shift doesn't work, Ctrl + Enter instead. I hope this helps, good luck.

As an addition to the previous answers. you will need to press Enter twice to run the code. In Python 3.7.3 and maybe above, pressing enter just takes you to a new line of the multi-line ipython input. In order to run the code, press Enter again.

Add "<SPACE>\" at the end of the first line before hitting <ENTER>.

It allows you to enter more lines and you can remove the " \" while editing the multiline-input as well. Execute the input as described before by pressing <ENTER> in the last (empty) line, which may require pressing <ENTER> 2x at the end of the last non-empty line.

If the input already executes while pressing <ENTER> anywhere in the last non-empty line of the input you can still insert lines anywhere by pressing <ENTER> at the end of the line above and you can add a line at the end by adding "<SPACE>\" at the end of the last line as before.

For some reason none of the above methods worked for me in a cmd shell on Windows, but this did the trick.

Use Ctrl + "O" before hitting Enter

I had tried the same for above code.

My suggestion is that rather using ipython shell, install jupyter notebook (pip install jupyter). then type jupyter notebook in the terminal rather than typing ipython. this command will open jupyter notebook in your default browser. here you can do whatever you want to do. you don't need have any internet connection. to stop the jupyter server, type ctrl+d in the terminal. you can watch the following tutorial on youtube: https://youtu.be/DKiI6NfSIe8

Enjoy.

Related