Python · Stage 2 – Doing Things with Values · Lesson 17 of 50

Getting User Input

Pause a program and collect text typed by the user.

name = input("What is your name? ")
Line-by-line explanation
  1. input displays a question and waits.
  2. The text inside brackets is the question.
  3. The user types an answer and presses Enter.
  4. name stores that answer as a string.

input() always returns a string, even if the person types digits.

age_text = input("Age: ")
Line-by-line explanation
  1. The prompt Age: appears.
  2. The user types an answer.
  3. age_text stores text such as "12".

Tiny Practice

Ask the user for a favorite food and store the answer.

Starter template

food = CHANGE_ME
print(food)
Line-by-line explanation
  1. The first line must ask a question.
  2. The second line displays the answer.
Hint
  1. Use input.
  2. Put the question inside quotes.
  3. Keep the result in food.
Show Solution
food = input("Favorite food: ")
print(food)
Line-by-line explanation
  1. input asks and waits.
  2. The answer is stored in food.
  3. print displays it.