Input and output

Martin McBride, 2017-01-31
Tags none
Categories none

Most modern computer programs use a Graphical User Interface (GUI) to communicate with the user. But for simpler programs, especially when you are first learning, it is easier to use a console interface.

Console interface

A console interface is a simple window where you can type commands or information in, and the computer program can display text information in response.

Examples of console windows are the Windows Command Prompt, the Linux Terminal window, or the Python Shell that comes with IDLE (the default Python environment). The Python Shell window is shown here:


When a program runs in a console, it can use the PRINT function to display text in the console window, and INPUT function to accept data typed in by the user.

Print functions

Here is how you might print "Hello World!"

PRINT("Hello World!")

{{% purple-note %}} In most languages, the functions which output text to the console window are called print functions. This is because, in the early days, computers didn't have screens. The main way a computer would output results would be via a printer. The name is still used out of tradition. {{% /purple-note %}}

Formatting output data

Sometimes you will need to format the data before you print it. You can do that using placeholders:

PRINT("It's the %s of %s, %s", "2nd", "Feb", 2017);

Which prints:

It's the 2nd of Feb, 2017

The first string passed to the PRINT function is called the format string. It contains 3 placeholders - {}. The remaining parameters are used to replace the placeholders in the final output string:

  • The parameter "2nd" replaces the first %s in the format string (%s expects a string).
  • The parameter "Feb" replaces the second %s in the format string.
  • The parameter 2017 replaces the %d in the format string (%d expects a number, which is converted to a string).

Input functions

The INPUT function allows the user to type some information into the console window.

You will usually want to use a PRINT function to ask the user for some information (so that they know what to type in). Then use an INPUT function so they can the information in. And then perhaps PRINT some kind of acknowledgement. A bit like a conversation.

Here is an example Python program that asks you your name, and says hello:

PRINT("What is your name?")
name = INPUT()
PRINT("Hello %s", name)

Copyright (c) Axlesoft Ltd 2021