Python · Stage 1 · Storing Information · Lesson 8 of 50

Naming Variables

Choose valid, readable variable names that explain what a value means.

A variable name is the label you choose for a stored value. Python has naming rules, and programmers use simple habits to make names readable.

A name may contain letters, numbers, and underscores. It cannot begin with a number. It cannot contain spaces or hyphens.

favorite_color = "green"
Line-by-line explanation
  1. favorite_color uses letters and an underscore, so it is valid.
  2. The underscore joins two words because spaces are not allowed.
  3. "green" is stored under that name.
student_age = 12
Line-by-line explanation
  1. student_age clearly describes the information.
  2. = stores a value.
  3. 12 is the stored value.

Tiny Practice: Improve a Name

Replace x with book_title everywhere.

Starter template

x = "The Hobbit"
print(x)
Line-by-line explanation
  1. The first line stores a title under an unclear name.
  2. The second line reads that same variable.
Hint
  1. Change the name on both lines.
  2. Use an underscore.
  3. Do not change the text.
Show Solution
book_title = "The Hobbit"
print(book_title)
Line-by-line explanation
  1. The first line uses a descriptive name.
  2. The second line uses the exact same spelling.
  3. The output remains The Hobbit.