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.
Code | Output |
---|---|
|
|
|
|
|
|
|
|
Concepts in Practice
The print() function
Do spaces really matter?
Spaces and newline characters are not inherently important. However, learning to be precise is an essential skill for programming. Noticing little details, like how words are separated and how lines end, helps new programmers become better.
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:
- A variable refers to a value stored in memory. In the statement above, variable can be replaced with any name the programmer chooses.
- 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.
- 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.
Concepts in Practice
The input() function
Try It
Frost poem
Write a program that uses multiple print()
statements to output the following poem by Robert Frost. Each print()
statement should correspond to one line of output.
Tip: You don't need to write the entire program all at once. Try writing the first print()
statement, and then click the Run button. Then write the next print()
statement, and click the Run button again. Continue writing and testing the code incrementally until you finish the program.
I shall be telling this with a sigh Somewhere ages and ages hence: Two roads diverged in a wood, and I-- I took the one less traveled by, And that has made all the difference.
Try It
Name and likes
Write a program that asks the following two questions (example input in bold):
What is your name? Shakira What do you like? singing
Output a blank line after reading the input. Then output the following message based on the input:
Shakira likes singing