🎓 A1Learner
🔍

Python User Input

User input is an answer a program receives from the person using it. The input() function pauses and waits for that answer.

Ask with a prompt

Type Sam and press Enter. The answer is stored in a variable named name.

name = input("What is your name? ")
print("Hello,", name)

Echo the answer

Type blue and press Enter. The stored answer prints back to you.

color = input("Favorite color? ")
print("You picked", color)

Numbers arrive as text

Type 5, then type 3. The plus joins the text, so you see 53.

first = input("First number? ")
second = input("Second number? ")
print(first + second)

Example

name = input("What is your name? ")
city = input("Which city do you live in? ")
print("Hello,", name)
print("You live in", city)

Key points

  • Prompt: the short message shown before you type
  • Use input() to read one line that the user types
  • Variable: a name that stores a value for later use
  • Use = to store the answer in a variable
  • The answer from input() is always a string
  • String: text, even when it looks like digits

In this example, the program was run with two typed answers. The first typed answer was Aisha. The second typed answer was Paris. The first line stores the name in the variable name. The second line stores the city in the variable city. The third line prints a greeting with the stored name. The last line prints where the user lives. You will learn casting in full in a later lesson.

Note

Text digits need int() to become numbers for maths.

Try it Yourself

Edit the code, then press Run.

Running is simulated · Python Playground

Exercise

What does input() always give you?