Sentry Answers>Python>

Read user input (STDIN) in Python

Read user input (STDIN) in Python

David Y.

The ProblemJump To Solution

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 function, which prompts the user to enter a line into stdin and returns it as a string once they press Enter. For example:

Click to Copy
name = input("Enter your name: ") print(f"Hi {name}!")

Running this script will look like this:

Click to Copy
$ 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 from Python’s built-in sys module. This allows us to treat stdin like a file. For example:

Click to Copy
import sys for line in sys.stdin: print(f"Received: {line.strip()}")

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

Click to Copy
python stdin_reader.py < lines.txt

For a lines.txt with the following content:

Click to Copy
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:

Click to Copy
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:

Click to Copy
import sys lines = sys.stdin.read() print(f"Received:\n{lines}")

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

Click to Copy
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:

Click to Copy
$ 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 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:

Click to Copy
import sys print(sys.argv)

We could use it as follows:

Click to Copy
$ python args.py 1 2 3 4 ['args.py', '1', '2', '3', '4']
  • Sentry BlogPython Performance Testing: A Comprehensive Guide
  • Sentry BlogLogging in Python: A Developer’s Guide
  • Syntax.fm logo
    Listen to the Syntax Podcast

    Tasty treats for web developers brought to you by Sentry. Get tips and tricks from Wes Bos and Scott Tolinski.

    SEE EPISODES

Loved by over 4 million developers and more than 90,000 organizations worldwide, Sentry provides code-level observability to many of the world’s best-known companies like Disney, Peloton, Cloudflare, Eventbrite, Slack, Supercell, and Rockstar Games. Each month we process billions of exceptions from the most popular products on the internet.

© 2024 • Sentry is a registered Trademark
of Functional Software, Inc.