# Python Read User Input from STDIN with input and sys

> Use the input function to prompt for a single line, sys.stdin to read multiple piped lines, or sys.argv to read command line arguments in a script

**URL:** https://sentry.io/answers/read-user-input-stdin-in-python/

---

## The Problem

In a Python script, how can I read from standard input (`stdin`)? For example, I want to prompt the user for their name and use it later on in my code.

## The Solution

Python provides a few methods for reading from `stdin` in different contexts. The simplest and most commonly used is the default [`input`](https://docs.python.org/3/library/functions.html#input) function, which prompts the user to enter a line into `stdin` and returns it as a string once they press Enter. For example:

```python
name = input("Enter your name: ")
print(f"Hi {name}!")
```

Running this script will look like this:

```bash
$ python main.py
Enter your name: Jane Doe
Hi Jane Doe!
```

If we want to read multiple lines from `stdin` and don't need to provide a prompt, we can use [`sys.stdin`](https://docs.python.org/3/library/sys.html#sys.stdin) from Python's built-in [`sys`](https://docs.python.org/3/library/sys.html) module. This allows us to treat `stdin` like a file. For example:

```python
import sys

for line in sys.stdin:
    print(f"Received: {line.strip()}")
```

We could then pipe a file (`lines.txt`) into our script:

```bash
python stdin_reader.py < lines.txt
```

For a `lines.txt` with the following content:

```
Many years later, as he faced the firing squad, Colonel Aureliano Buendía was to remember that distant afternoon when his father took him to discover ice.
The sky above the port was the color of television, tuned to a dead channel.
It was a bright cold day in April, and the clocks were striking thirteen.
```

We would get this output:

```
Received: Many years later, as he faced the firing squad, Colonel Aureliano Buendía was to remember that distant afternoon when his father took him to discover ice.
Received: The sky above the port was the color of television, tuned to a dead channel.
Received: It was a bright cold day in April, and the clocks were striking thirteen.
```

As with a file, we could also read all the data at once instead of going line by line, using `sys.stdin.read`:

```python
import sys

lines = sys.stdin.read()
print(f"Received:\n{lines}")
```

Using `lines.txt` again, this would produce the output below:

```
Received:
Many years later, as he faced the firing squad, Colonel Aureliano Buendía was to remember that distant afternoon when his father took him to discover ice.
The sky above the port was the color of television, tuned to a dead channel.
It was a bright cold day in April, and the clocks were striking thirteen.
```

Note that in both cases, we could enter lines into `stdin` interactively as we would with `input` instead of piping a file into the script. If we run our script without piped input, it will wait for us to enter something into `stdin`. We can then type or paste in multiple lines, finishing with `Ctrl-D`:

```
$ python stdin_reader.py
Many years later, as he faced the firing squad, Colonel Aureliano Buendía was to remember that distant afternoon when his father took him to discover ice.
The sky above the port was the color of television, tuned to a dead channel.
It was a bright cold day in April, and the clocks were striking thirteen.
^D
Received:
Many years later, as he faced the firing squad, Colonel Aureliano Buendía was to remember that distant afternoon when his father took him to discover ice.
The sky above the port was the color of television, tuned to a dead channel.
It was a bright cold day in April, and the clocks were striking thirteen.
```

Finally, [`sys.argv`](https://docs.python.org/3/library/sys.html#sys.argv) provides a list of command line arguments passed to our script upon execution, starting with the script name. For example, the script below will print a list of its arguments:

```python
import sys

print(sys.argv)
```

We could use it as follows:

```
$ python args.py 1 2 3 4
['args.py', '1', '2', '3', '4']
```

---

*Source: [sentry.io/answers/read-user-input-stdin-in-python/](https://sentry.io/answers/read-user-input-stdin-in-python/)*
