Login
📚 Introduction to Python Programming
Chapters ▾
⇩ Download ▾

1.2 Input/output

Learning objectives

By the end of this section you should be able to

  • Display output using the print function.
  • Obtain user input using the input function.

Basic output

The print function displays output to the user. Output is the information or result produced by a program. The sep and end options can be used to customize the output. Table 1.1 shows examples of sep and end.

Multiple values, separated by commas, can be printed in the same statement. By default, each value is separated by a space character in the output. The sep option can be used to change this behavior.

By default, the print function adds a newline character at the end of the output. A newline character tells the display to move to the next line. The end option can be used to continue printing on the same line.

Table 1.1 Example uses of print.
CodeOutput
print("Today is Monday.")
print("I like string beans.")
Today is Monday.
I like string beans.
print("Today", "is", "Monday")
print("Today", "is", "Monday", sep="...")
Today is Monday
Today...is...Monday
print("Today is Monday, ", end="")
print("I like string beans.")
Today is Monday, I like string beans.
print("Today", "is", "Monday", sep="? ", end="!!")
print("I like string beans.")
Today? is? Monday!!I like string beans.

Basic input

Computer programs often receive input from the user. Input is what a user enters into a program. An input statement, variable = input("prompt"), has three parts:

  1. A variable refers to a value stored in memory. In the statement above, variable can be replaced with any name the programmer chooses.
  2. The input function reads one line of input from the user. A function is a named, reusable block of code that performs a task when called. The input is stored in the computer's memory and can be accessed later using the variable.
  3. A prompt is a short message that indicates the program is waiting for input. In the statement above, "prompt" can be omitted or replaced with any message.