University of Free Knowledge
QA 76.73 · fol. 3

Print Out, Input In

print() sends text to the screen while input() reads one line back from the person, always handing it over as a string. · 10 min

So far your programs have only talked to you. Now they can listen. A program has two channels to a person: print(...) sends text out to the screen, and input() reads one line back in — everything the person types until they press Enter. These two calls turn a fixed program into a conversation. There is one detail that trips nearly everyone the first time, and it is worth meeting head-on: whatever the person types, input() always hands it back as text, never as a number.

Guess before you learn

A program runs age = input() and you type 17 and press Enter. What is the type of the value stored in age?

THE DEPTH DIAL — the same idea, younger or deeper
9–12

9–12

print and input are your program's two channels to a person: print writes a line to standard output; input reads a line from standard input and returns it with the trailing newline stripped. The return type is invariably str. This is deliberate — the program cannot know whether the characters you typed were meant as a number, a name, or a command, so it hands back the raw text and lets you decide. Converting is an explicit act: int("17") gives 17, but int("cat") raises a ValueError, which is Python honestly reporting that the text was not a number. Read the input, convert it, and check it — three separate responsibilities you will meet again and again.

input()

A call that pauses the program, reads one typed line, and returns it as text — a str, never a number.

the screen shows the questionyou type and press Enterprint("What is your name?")name = input()print("Hello, " + name)
PLATE I Ask, wait, answer: output, then input, then output.

Ink That Thinks — guess first; the answer draws itself.
This program greets you by name. Drag the four events into the order they happen when it runs. print("What is your name?") name = input() print("Hello, " + name)

  1. The screen shows: What is your name?
  2. The program pauses and waits for you to type
  3. You type Ada and press Enter
  4. The screen shows: Hello, Ada
Reorder, then commit.
PLATE II The order of a short conversation — guess in graphite, truth in ink.
Retrieval Gate — answer before you continue 0 / 4

1.What type does input() always return?

2.What does print("Hi") do?

3.Put the lines of this program in the order they run.

  1. print("Enter a number:")
  2. n = input()
  3. print("Thanks!")

4.You run n = int(input()) and type 5. On the next line, print(n + 2) runs. What number appears?

The fix for the input surprise is one word wrapped around the other: int(input()). The input() reads the line as text, and int(...) turns that text into a whole number. Use float(...) instead when you expect a decimal. And if the person types something that is not a number at all, like cat, the conversion raises a ValueError — Python telling you, honestly, that the text was not a number. That is not the program breaking; that is the program refusing to guess.

Trace an input-and-add program — the steps fade as you master them

1
Line 1: x = input() and you type 8. What value does x hold, and what type is it?
x holds the text "8" (a str)
2
Line 2: y = int(x). This converts the text to a number. What value does y hold now?
y holds the number 8 (an int)
3
Line 3: print(y + 1). Now the addition is between two numbers. What appears on the screen?
9
YOU TYPEINPUT() RETURNSITS TYPEAFTER INT(...)8"8"str825"25"str25cat"cat"strValueError
PLATE III input() always returns text; int() turns numeric text into a number — or refuses.
Why is this true?

Why does input() return text even when you clearly typed a number?

Because the program cannot know what you meant the characters to be — a count, a name, a code. It hands back the exact text you typed and leaves the decision to convert to you, using int() or float() when a number is what you intend.

Retrieval Gate — answer before you continue 0 / 4

1.Why does age = input() followed by print(age + 1) cause an error?

2.How do you read a whole number from the person in a single line?

3.In one sentence: what does input() give back, and what must you do before using it in arithmetic?

4.Without looking: what does print do, what does input do, and what type does input return?

Note

Forgetting to convert input? The Atelier of Mind drills the read-then-convert habit until it is automatic.

Practice — new ink and old, interleaved

1.In your own words: what does an assignment like name = value do?

2.Review from folio 2: what is the type of "7", written with quotes?

3.Which line reads a decimal number, like 3.5, from the person?

4.Review from folio 1: put these lines in the order they run.

  1. print("Ready?")
  2. answer = input()
  3. print("Go!")

5.You run n = int(input()) and type 6. Then print(n * 10) runs. What number appears?

6.Why does "3" + 5 cause an error?

7.What is the type of "7" — the 7 written inside quotes?

The Call Slip — search everything Ctrl·K / ⌘K