Module 0 · Functions, Variables · Lesson 2 of 5

Variables and input

A variable is a name that holds a value so you can use it later. The = sign is assignment: Python works out the right side first, then stores it in the name on the left.

name = input("What's your name? ")
print("hello,", name)

input shows a prompt, waits for the user to type, and returns what they typed. It always returns a str, even when the user types digits.

There are a few ways to combine text:

  • "hello, " + name glues strings together (you add the space yourself).
  • print("hello,", name) passes two arguments, so print adds the space.

Pick names that say what they hold: name, total, price. Future you will thank you.

When you click Run below, a cursor appears in the terminal when your program asks for input. Type and press Enter.

Try it

Edit freely · Ctrl+Enter runs

Quick check0 of 3 right

The user types 42 for x = input(). What type is x?

What does this print? x = "cat" x = "dog" print(x)

What does print("hi" + "there") show?

Practice0 of 2 solved

1. Excited echoTo do

Ask the user for some text, then print it back followed by !!! with no space before the exclamation marks.

InputOutput
pythonpython!!!
Hi thereHi there!!!

2. Name cardTo do

Ask for a first name, then a last name (two separate input calls). Print them as Last, First.

InputOutput
Ada ⏎ LovelaceLovelace, Ada
Grace ⏎ HopperHopper, Grace