input

We can use input to get typed input from the user:

name = input('What is your name? ')
print(name)
quest = input('What is your quest? ')
print(quest)
What is your name? King Arthur
King Arthur
What is your quest? To seek the holy grail!
To seek the holy grail!

NOTE


The bold text above was typed by the user---it wasn't printed by the program.

The argument you give to input is a message the program will use to ask the user to type something. After the user presses Enter, input will give whatever the user typed. On the first line, we tell program to:

  1. Ask the user 'What is your name? '
  2. Wait for the user to type something and press Enter
  3. Store whatever the user typed in the variable name

Look at the difference between these two lines:

color = input("What is your favorite color?")
color = input("What is your favorite color? ")
What is your favorite color?Blue
What is your favorite color? Blue

input will not automatically put a space after the message you give it. You have to put a space after the question mark on your own.


previous: variables next: exercises