Course outline · 0% complete

0/27 lessons0%

Course overview →

Programs take input

lesson 1-3 · ~9 min · 3/27

Reading what the user types

Your Hello, world program from lesson 1-2 only produced output. Real programs also take input, the first corner of the input → process → output triangle from lesson 1-1.

Python's built-in instruction for input is input(). When the interpreter reaches input(), it pauses, waits for a line of typed text, and then hands that text to your program.

In this editor, you type the input ahead of time into the Input (stdin) box below the code. "stdin" is short for standard input, the traditional name for the stream of text a program reads. When your code runs, input() reads from that box.

One more trick: print can show several things at once if you separate them with a comma. It automatically puts a space between them:

print("You typed:", input())

Read it inside-out: input() grabs the typed text first, then print shows the label and the text together.

Code exercise · python

The Input box already contains the text "hi there". Press Run and see how input() picks it up.

Quiz

What does input() do when the program runs?

Code exercise · python

Your turn. The Input box contains the word banana. Write a program that reads that input and prints exactly: Echo: banana (so it would echo whatever word is in the box).