Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to give jupyter cell standard input in python?

I am trying to run a program on a jupyter notebook that accepts user input, and I cannot figure out how to get it to read standard input. For example, if I run the code with shift-enter:

a = input() print(a) 

the cell indicates it is running, but does not accept input from me. How do I get it to accept input?

like image 415
Alex Avatar asked Jan 23 '16 19:01

Alex


People also ask

Can we take input in Jupyter Notebook?

By using Jupyter Notebook in this example, it makes it really easy to see what the prompt looks like when calling that input() function. We can type some characters into the prompt for our name. Then, we can add the code to print() that variable. We might want to make that output just a little more interesting.

What is %% capture in Python?

Capturing Output With %%capture IPython has a cell magic, %%capture , which captures the stdout/stderr of a cell. With this magic you can discard these streams or store them in a variable.


1 Answers

Use the raw_input() (for Python 2) or input() (for Python 3) methods.

Example code:

a = raw_input() print(a) 

Example notebook:

like image 86
Rahul Tripathi Avatar answered Sep 28 '22 08:09

Rahul Tripathi