Python: Variables, Strings and Numbers

1. Variables: What They Look Like

Go to Chapter 2 of A Smarter Way.

Variable names can contain letters, numbers and underscores – nothing else.

And variable names must always begin with a letter or underscore, but never a number.

Variable names can never, ever have spaces in them!

2. Variables: Strings

When you assign a string value to a variable (or create a variable and simultaneously assign the value), remember that strings go in quotes.

name = "Barney"

Note the spaces around the equal sign (which is actually the assignment operator).  These are not absolutely required, but again, they’re dictated by Python’s stern style requirements. So use them. Always.

As for the quotation marks, you can use single quotes or double quotes; they both mean the same thing in Python.

You can print out the values of variables like this:


You can’t put variables inside quotes: they must always be in the clear.

print("name") # NO this is wrong!

print(name) # YES do it this way


Online, go to http://introtopythonorg/hello_world.html. Run the simple examples.

Go to and review the exercises.

  1. Continue editing your script,
  2. Create a new variable that contains “Author: “.
  3. Create a new variable, my_name, and populate it with your name (or any name).
  4. Be sure to handle it as a string.
  5. Add another print statement that outputs these variables on one line.

3. Variables: Numbers

Go to Chapter 3 of A Smarter Way.

When you’re assigning a value to a variable, and the value is a number, numbers never go in quotes.

number_of_cats = 2

You can print a numeric variable like this:


If a variable is a number, you can use the variable like a number in math:

number_of_cats = number_of_cats + 3


Go to and review the exercises.

  1. Continue editing your
  2. Create a new variable, my_year,  that contains the current year.
  3. Be sure to handle the variable as a number.
  4. Print “Copyright ” and  the year.

By now your should be printing what looks like the beginning of a paper: a title, an author and a year.

4. Now go to Variables, Strings and Numbers online:

Particularly look at Changing case and Comments, for now. We’ll look at concatenating strings later.

my_name = "blarney"



# This is a comment

print(my_name) # This is an inline comment

This is a 
multi-line comment


At the bottom of the page, note the section, Exploring the Python Community

  1. Continue editing your file.
  2. Print the label “Author’s name: ” followed by my_name in lowercase, uppercase and title case. Use three lines to do this.
  3. Create a new variable, my_degree, that contains the value ” Ph.D.”.
  4. Concatenate my_name and my_degree so you can print them. Do you need another variable or can you concatenate inside the print statement?