Hello World!

Step 4 – Storing your name

So, we can get the user to enter a name, but we can't do anything else with it because we haven't stored it.

We need to use a variable...

The way to store something in a variable in Python is easy.

Code helper image
  1. Change your line of code to read   userName = input("What's your name? ")
  2. Run your code (Run > Run Module)

The program still doesn't do anything else, but we do know it's stored the name in the variable called userName.

THEORY: a variable is a named area of memory where we can store a value. Our variable is called userName. We can use the name to recall the variable and to do other things with it.

Code helper image

Lets prove that the variable has been stored by printing it out.

  1. Add a new line to your code which reads   print(userName)
  2. Run the program and the name you entered should be displayed on screen

But we can do better than that.

Code helper image
  1. Change your code so that it says   print("Hello", userName)
  2. Run the program and see what happens

The comma lets us print some text and then a variable.

  1. Try changing your code again to read   print("Hello", userName, "I hope you're having a nice day.")
  2. Run the program and see what happens now

The comma is really useful when you're using variables.