1. Reading from standard input
    1. What are standard streams?
    2. Reading a single line
    3. Reading input with a prompt
    4. Checking if input is over

Reading from standard input

What are standard streams?

When a program runs, it has three ways of communicating with its virtual environment:

  • Standard input stream (stdin)
  • Standard output stream (stdout)
  • Standard error stream (stderr)

These streams allow data to flow. The input stream brings data into the program. The output and error streams bring data out to the virtual environment and users.

There are many other ways for information to enter and exit a program, but these streams are standard. A programmer who wants their code to work with multiple sources of data can read from standard input and write to standard output without worrying specifically about where the input comes from and where the output goes. Primarily, the data fed to standard input comes from these sources:

  • The contents of a file
  • Manual entry from a user running the program in a system console
  • The output of another program

Just by learning how to use standard streams, you can create programs that work with all kinds of interesting data!

Reading a single line

Consider this input stream. It could be entered into standard input by a user or redirected from a file. No matter what the source, your program can read it and work with the contents using the same code:

Input Stream

Dear Rosa,
This is a letter I send to you at
an urgent time. The contents are
very urgent and I wanted to send
...

The input() method reads a single line from standard input and returns it as a string.

Program Code

word = input()
# word = 'Dear Rosa,'

If the program calls input() again, it will return the next line as a string.

word = input()
next_word = input()
# next_word = 'This is a letter I send to you at'

Reading input with a prompt

Many people write and run programs in a system console or terminal. Software engineers, database administrators, scientists, and even journalists use such programs to access data and accomplish a wide range of computational chores.

You can think of these programs as a kind of chat bot!

However, without a message from the program, the users will not know how many lines of input the program expects and what data they are supposed to contain.

Luckily, the input() method takes a string argument for a prompt can be displayed to a user.

Program Code

# pass the prompt to the input method
name = input('What is your name? ')
# name = 'whatever the first line of stdin is, as a string'

The extra space after the question mark is intentional, it gives the user room before they start typing their input. The user’s cursor starts at the end of that line in the system output. In the system console, they might see something like this:

System Console Output

$ python programname.py
What is your name? Marcello

To make the user start typing on the next line, you can end the prompt with the newline character: \n.

Program Code

# pass the prompt to the input method
name = input('What is your name?\n')
# name = 'whatever the first line of stdin is, as a string'

System Console Output

$ python programname.py
What is your name?
Marcello

If standard input is coming from a terminal user, the program will wait until a value has been entered. When the user hits enter, creating a newline, the value they entered is returned from input().

If standard input is coming from an automated source like a file, the wait is only as long as it takes to read the next line. Though the prompt message is not necessary when reading from a file, it may help label the incoming information if the lines of the file contain different data.

Checking if input is over

When you learn more about conditions and loops, you will be able to create programs that work with large streams of data. Here are some common ways to know when to stop reading from standard input (when to stop calling input()):

  • Check for a stop line, such as 'stop' or 'quit'
  • Check if the most recent input line is empty (length of zero)
  • Check for an error when reading the input stream

As you learn more syntax, you will be able to implement these techniques. But for now, input() is still a powerful method that allows you to bring a known number of lines of data into your programs.